r 光栅“.self$finalize”错误导致失败

Hey*_*ans 7 r raster r-sf

我已经使用以下函数通过 sf 多边形剪辑栅格至少一年了,没有任何问题,但自从更新到 R 4.0.3 和最近的 rgdal 更新以来,我开始遇到麻烦。当我第一次运行 Clip 时,出现错误,指出 x$.self$finalize())() 并且函数失败。在我再次运行它或重新加载光栅包后,它似乎可以工作。这是之前通过 raster 和 sp 提出的。在这种情况下,问题似乎相关,请参见下文:我留下了通常在脚本中加载的附加库,以防它们与问题相关。

library(raster)
library(EBImage)
library(sf) 
library(stars)
library(reticulate)
library(fitdistrplus)
measure <- import("skimage.measure")
clip<-function(x,shape) {
  # Arguments:
  #   x: Input raster
  #   shape: polygon (in same projection)
  # Returns:
  #   clipped raster
  #######
  return(crop(mask(x, shape), shape))
}
## polygon w/ single feature
shoreline <- st_read("shoreline_FULL.shp")

## Load channel network mask
DCN <- raster("2000_07_channel_clean.tif")
DCNt <- t(as.matrix(DCN))

Run Code Online (Sandbox Code Playgroud)

然后运行剪辑时:

> DCN <- clip(DCN, shoreline)
Error in (function (x)  : attempt to apply non-function
Called from: (function (x) 
x$.self$finalize())(<environment>)
Browse[1]> DCNt <- t(as.matrix(DCN))
Error during wrapup: no method for coercing this S4 class to a vector
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]> 
Run Code Online (Sandbox Code Playgroud)

更奇怪的是,当我简单地运行等效命令时,会返回错误,但结果会计算并且现在剪辑可以工作。乏味,我需要能够获取整个脚本的来源!

> test <- crop(mask(DCN, shoreline), shoreline)
Error in x$.self$finalize() : attempt to apply non-function
Error in x$.self$finalize() : attempt to apply non-function
Error in (function (x)  : attempt to apply non-function
> DCN <- clip(DCN, shoreline)
> 
Run Code Online (Sandbox Code Playgroud)

示例中使用的 shapefile 和栅格可以在此处找到: https: //drive.google.com/drive/folders/1D_y_9vAA69CsA2nwJTw_hpfdjgZfqdRm ?usp=sharing

并供参考:

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
 [1] fitdistrplus_1.1-1 survival_3.2-7     MASS_7.3-53       
 [5] reticulate_1.18    stars_0.4-3        abind_1.4-5        sf_0.9-6          
 [9] EBImage_4.31.0     raster_3.4-5       sp_1.4-4          

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5          locfit_1.5-9.4      lattice_0.20-41     deldir_0.1-29      
 [5] FNN_1.1.3           fftwtools_0.9-9     png_0.1-7           class_7.3-17       
 [9] zoo_1.8-8           gtools_3.8.2        digest_0.6.27       gstat_2.0-6        
[13] R6_2.4.1            tiff_0.1-5          R.matlab_3.6.2      pracma_2.2.9       
[17] coda_0.19-4         e1071_1.7-4         spam_2.5-1          pillar_1.4.6       
[21] rlang_0.4.8         spdep_1.1-5         rstudioapi_0.11     gdata_2.18.0       
[25] gmodels_2.18.1      R.utils_2.10.1      R.oo_1.24.0         Matrix_1.2-18      
[29] splines_4.0.3       rgdal_1.5-18        htmlwidgets_1.5.2   igraph_1.2.6       
[33] RCurl_1.98-1.2      compiler_4.0.3      pkgconfig_2.0.3     BiocGenerics_0.35.4
[37] htmltools_0.5.0     tidyselect_1.1.0    tibble_3.0.4        expm_0.999-5       
[41] intervals_0.15.2    codetools_0.2-16    spacetime_1.2-3     crayon_1.3.4       
[45] dplyr_1.0.2         rappdirs_0.3.1      bitops_1.0-6        R.methodsS3_1.8.1  
[49] grid_4.0.3          jsonlite_1.7.1      nlme_3.1-150        spData_0.3.8       
[53] lwgeom_0.2-5        lifecycle_0.2.0     DBI_1.1.0           magrittr_1.5       
[57] units_0.6-7         KernSmooth_2.23-17  LearnBayes_2.15.1   ellipsis_0.3.1     
[61] xts_0.12.1          generics_0.0.2      vctrs_0.3.4         boot_1.3-25        
[65] tools_4.0.3         glue_1.4.2          purrr_0.3.4         maps_3.3.0         
[69] fields_11.6         jpeg_0.1-8.1        parallel_4.0.3      classInt_0.4-3     
[73] dotCall64_1.0-0  
Run Code Online (Sandbox Code Playgroud)

Rob*_*ans 10

这些烦人的短信

Error in x$.self$finalize() : attempt to apply non-function
Error in (function (x)  : attempt to apply non-function
Run Code Online (Sandbox Code Playgroud)

如果您将 terra 包更新到 CRAN 上的当前二进制版本,该信息将会消失。如果您自己编译,请确保也使用 Rcpp 的当前版本。