r Raster ".self$finalize" 错误导致失败

7
我至少已经使用以下函数一年来通过sf多边形剪切栅格,没有任何问题,但自从更新到R 4.0.3和最近的rgdal更新后,我开始遇到麻烦。当我第一次运行clip时,会出现一个错误,指出x$.self$finalize())(),然后函数失败。在我再次运行它或重新加载raster包后,它似乎可以工作。 这个问题之前已经被提到过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))


运行剪辑时:
> 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]> 

更为奇怪的是,当我只运行等效命令时,会返回错误,但结果计算出来了,现在剪贴板可以正常工作。这很繁琐,我需要能够从整个脚本中获取源代码!
> 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)
> 

这个示例中使用的shapefile和raster可以在这里找到: 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  

1
你能提供一个自包含的可重现实例吗?另外,这个:return(mask(crop(x, shape), shape)) 应该更高效(先裁剪)。 - Robert Hijmans
我上传了数据以便能够重现错误。谢谢你的提示! - Heymans
来这里找到了关于 x$.self$finalize() 的答案,这与我正在处理的栅格非常相似。通过移除 R 4.0.3 及其所有包并重新安装,已经在本地解决了该问题。现在我在 shinyapps.io 上仍然遇到了同样的问题。 - LaurensP
1
@LaurensP:好知道!这是一个简单但不理想的解决方案。我认为这个问题与最近raster和gstat之间的故障有关,因为在安装gstat后,如果gstat在命名空间中或反之,则有时library(raster)会失败。请参见:https://github.com/r-spatial/gstat/issues/47 - Heymans
2个回答

10

这些令人烦恼的消息

Error in x$.self$finalize() : attempt to apply non-function
Error in (function (x)  : attempt to apply non-function

如果您将terra软件包更新到CRAN上的当前二进制版本,则此问题将消失。如果您自己编译,请确保还使用当前版本的Rcpp。


1
我正在维护一个仍在使用“raster”软件包而不是现代“terra”软件包的软件。有没有其他解决此类错误的方法?向你致敬,Robert,感谢你的所有工作。 - Abel Callejo
1
如果您安装当前CRAN版本的软件包,则错误将消失。 - Robert Hijmans

0

我遇到了类似的问题,受评论的启发,我尝试分离gstat包。这样做后,错误消息消失了。

然而,我在使用标准的detach()命令时失败了,不得不应用kohske作为回答给出的自定义函数,解决了这个问题:如何在不重启R的情况下卸载一个包


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接