尝试保存 R 脚本而不是执行整个代码

问题描述

醒来发现一个奇怪的新问题。对于我的一个脚本,按 CTRL+S 保存被解释为 SHIFT+CTRL+S 并保存,同时还获取活动文件(即运行整个文件)。在 RStudio 中手动按下保存按钮具有相同的效果。即使在这个脚本中,这也是新行为,我没有在其他地方找到任何关于脚本中可能导致问题的信息。我已经更新了 R、RStudio 和一些没有效果的软件包。我没有收到任何错误消息,但经常使用 CTRL+S 进行保存是我工作流程中非常本能的部分 - 知道是什么把它搞砸了吗?

这是我的会话信息:

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  Grdevices utils     datasets  methods   base     

other attached packages:
 [1] rgdal_1.5-23       rJava_1.0-4        rgeos_0.5-5        dismo_1.3-3       
 [5] raster_3.4-13      mapdata_2.3.0      maps_3.3.0         maptools_1.1-1    
 [9] sp_1.4-5           ggx_0.1.1          ggridges_0.5.3     RColorBrewer_1.1-2
[13] ggsci_2.9          ggpubr_0.4.0       forcats_0.5.1      stringr_1.4.0     
[17] dplyr_1.0.7        purrr_0.3.4        readr_1.4.0        tidyr_1.1.3       
[21] tibble_3.1.2       ggplot2_3.3.5      tidyverse_1.3.1   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.6        lubridate_1.7.10  lattice_0.20-44   assertthat_0.2.1 
 [5] utf8_1.2.1        R6_2.5.0          cellranger_1.1.0  plyr_1.8.6       
 [9] backports_1.2.1   reprex_2.0.0      httr_1.4.2        pillar_1.6.1     
[13] rlang_0.4.11      curl_4.3.2        readxl_1.3.1      rstudioapi_0.13  
[17] data.table_1.14.0 car_3.0-11        foreign_0.8-81    munsell_0.5.0    
[21] broom_0.7.8       compiler_4.1.0    modelr_0.1.8      pkgconfig_2.0.3  
[25] tidyselect_1.1.1  codetools_0.2-18  rio_0.5.27        fansi_0.5.0      
[29] Crayon_1.4.1      dbplyr_2.1.1      withr_2.4.2       grid_4.1.0       
[33] jsonlite_1.7.2    gtable_0.3.0      lifecycle_1.0.0   DBI_1.1.1        
[37] magrittr_2.0.1    scales_1.1.1      zip_2.2.0         cli_2.5.0        
[41] stringi_1.6.2     carData_3.0-4     ggsignif_0.6.2    fs_1.5.0         
[45] xml2_1.3.2        ellipsis_0.3.2    generics_0.1.0    vctrs_0.3.8      
[49] openxlsx_4.2.4    tools_4.1.0       glue_1.4.2        hms_1.1.0        
[53] abind_1.4-5       colorspace_2.0-2  rstatix_0.7.0     rvest_1.0.0      
[57] haven_2.4.1 

这是我的脚本。要点是我有 42 个物种的坐标数据框,对于每个物种,我只需要在每个 1 平方公里的网格单元内采样一个点。我今天添加了循环,所以我猜这可能是触发它的原因?


library(tidyverse)
library(ggplot2)
library(ggpubr)
library(ggsci)
library(RColorBrewer)
library(ggridges)
library(ggx)
library(maptools)
library(maps)
library(mapdata)
library(dismo)
library(rgeos)
library(rJava)
library(raster) 
library(rgdal)
library(sp)

# read in full dataset of all distributions
data <- read.csv("final_dists_ALL_20Apr2021.csv",header = TRUE,sep = ",",na.strings = "")
data$species <- as.factor(data$species)
data$coordinateUncertaintyInMeters <- as.numeric(data$coordinateUncertaintyInMeters)

#### TEST #####

# make a test subset for costaricanum
test <- data %>% filter(species == "costaricanum" & 
(coordinateUncertaintyInMeters <= 1000 | is.na(coordinateUncertaintyInMeters)))

# turn coordinates into SpatialPointsDataFrame object
coordinates(test) <- ~ decimalLongitude + decimalLatitude

# create a RasterLayer with the extent of the study area
r <- raster(test)

# set the resolution of the cells to 1 km
res(r) <- 0.00001

# expand (extend) the extent of the RasterLayer a little
r <- extend(r,extent(r) + 1)

# sample
test_sample <- gridSample(test,r,n = 1)

# attach data info to subsample coords
test <- data %>% filter(species == "costaricanum")

test_sample <- data.frame(test_sample)

sample <- left_join(test_sample,test,by = c("decimalLongitude" = "decimalLongitude","decimalLatitude" = "decimalLatitude"))

sample <- sample %>% distinct(decimalLongitude,decimalLatitude,.keep_all = TRUE)

##### LOOP #####

species_list <- levels(data$species)

for (i in 1:length(species_list)) {
  
  test <- data %>% filter(species == species_list[i] & 
   (coordinateUncertaintyInMeters <= 1000 | is.na(coordinateUncertaintyInMeters)))
  
  coordinates(test) <- ~ decimalLongitude + decimalLatitude

  r <- raster(test)
  res(r) <- 0.00001
  r <- extend(r,extent(r) + 1)

  test_sample <- gridSample(test,n = 1)
  
  test <- data %>% filter(species == species_list[i])
  test_sample <- data.frame(test_sample)
  sample <- left_join(test_sample,"decimalLatitude" = "decimalLatitude"))
  
  sample <- sample %>% distinct(decimalLongitude,.keep_all = TRUE)
  
  write.csv(sample,paste0("1km_sub_",species_list[i],".csv"),row.names = F)
  
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)