I've been using the following function to clip raster's by sf polygons for at least a year without any issues, but have started running into trouble with it since updating to R 4.0.3 and the recent rgdal updates. When I run clip the first time, I get an error stating x$.self$finalize())() and the function fails. After I run it again or reload the raster package it seems to work. This was previously raised with raster and sp. In this case the issue seems related, see below: I left the additional libraries which are normally loaded in the script in case they are relevant to the issue.
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))
When then running clip:
> 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]>
Even more oddly, when I simply run the equivalent commands the error is returned but the result computes and now clip works. Tedious and I need to be able to source the entire script!
> 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)
>
The shapefile and raster used in the example can be found here: https://drive.google.com/drive/folders/1D_y_9vAA69CsA2nwJTw_hpfdjgZfqdRm?usp=sharing
And for reference:
> 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
These annoying messages
Error in x$.self$finalize() : attempt to apply non-function
Error in (function (x) : attempt to apply non-function
Will disappear if you update the terra package to the current binary version on CRAN. If you compile yourself, make sure to also use the current version of Rcpp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With