Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event caching of SpatRaster triggers "Error in .External(list(name = "CppMethod__invoke_notvoid",..." AFTER restarting R session #283

Open
CeresBarros opened this issue Feb 11, 2023 · 1 comment

Comments

@CeresBarros
Copy link
Member

I only see the error after restarting the R session and re-running the same code (this is, just rerunning the same call won't show the error).

pkgPath <- .libPaths()[1]

if (!"Require" %in% installed.packages(lib.loc = pkgPath) ||
    packageVersion("Require", lib.loc = pkgPath) < "0.2.6.9008") {
  remotes::install_github("PredictiveEcology/Require@74d50ea4b62ca05e805e10f8567db41b76202dee",
                          upgrade = FALSE, force = TRUE)
}

library(Require)

Require(c("CeresBarros/reproducible@945bc104c053ac3675b7e9184c85d799aff00960 (HEAD)",
          "CeresBarros/SpaDES.tools@terra-migration (HEAD)",
          "PredictiveEcology/LandR@terra-migration (HEAD)",
          "PredictiveEcology/SpaDES.core@1c52a5a0d30d30877fc1a18f83b0cfbd3cdd21d7 (HEAD)",
          "terra"
))

tempPath <- "~/test"
cPath <- file.path(tempPath, "cache")
dPath <- file.path(tempPath, "inputs")
mPath <- file.path(tempPath, "mod")
dir.create(cPath, recursive = TRUE)
dir.create(dPath, recursive = TRUE)
dir.create(file.path(mPath, "testModule"), recursive = TRUE)

options("reproducible.useNewDigestAlgorithm" = 2,
        "reproducible.useCache" = TRUE,
        "reproducible.cachePath" = cPath,
        "reproducible.destinationPath" = dPath, 
        "reproducible.useGDAL" = FALSE,
        "reproducible.cacheSaveFormat" = "rds",
        "reproducible.useMemoise" = TRUE,
        "reproducible.useTerra" = TRUE,
        "reproducible.rasterRead" = "terra::rast",
        "spades.inputPath" = dPath,
        "spades.useRequire" = FALSE)

cat(
  "defineModule(sim, list(
  name = 'testModule',
  timeunit = 'year',   ## NOT ADDING THIS CREATES AN ERROR.
  parameters = bindrows(
    defineParameter('.useCache', 'character', 'init', NA, NA,
                    desc = 'Controls cache; caches the init event by default')
  ),
  outputObjects = bindrows(
    createsOutput('CASFRIRas', 'SpatRaster',
                  desc = 'blah'))
))

doEvent.testModule = function(sim, eventTime, eventType) {
  switch(
    eventType,
    init = {
      sim <- Init(sim) # initialize
    },
    )
  return(sim)
}

Init <- function(sim) {
  studyArea <- vect(randomStudyArea(size = 10000000000, seed = 123))
  SAname <- studyAreaName(studyArea)
  
  ## proj to CASFRI
  # studyArea <- project(studyArea, y = '+proj=lcc +lat_0=49 +lon_0=-95 +lat_1=49 +lat_2=77 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs')
  RTM <- tryCatch(rast(resolution = 250, extent = ext(studyArea), crs = crs(studyArea, proj = TRUE)))
  
  CASFRItiffFile <- asPath(file.path(dPath, 'Landweb_CASFRI_GIDs.tif'))
  CASFRIattrFile <- asPath(file.path(dPath, 'Landweb_CASFRI_GIDs_attributes3.csv'))
  CASFRIheaderFile <- asPath(file.path(dPath, 'Landweb_CASFRI_GIDs_README.txt'))
  url <- 'https://drive.google.com/file/d/1y0ofr2H0c_IEMIpx19xf3_VTBheY0C9h'
  
  sim$CASFRIRas <- Cache(prepInputs, 
                         targetFile = basename(CASFRItiffFile), 
                         archive = asPath('CASFRI for Landweb.zip'), 
                         url = url, 
                         alsoExtract = c(CASFRItiffFile, CASFRIattrFile, CASFRIheaderFile), 
                         studyArea = studyArea,
                         rasterToMatch = RTM,
                         method = 'bilinear', 
                         datatype = 'INT4U', 
                         filename2 = NULL, 
                         overwrite = TRUE, 
                         userTags = c('CASFRIRas', 'stable'))
  return(sim)
}

  ", file = file.path(mPath, "testModule", "testModule.R")
)

setPaths(cachePath = cPath, modulePath = mPath, inputPath = dPath)
sim <- simInit(times = list(start = 1, end = 1), modules = "testModule")
out <- spades(sim)
out$CASFRIRas

## restart R and re-run to see error

session info

> sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.utf8  LC_CTYPE=English_Canada.utf8    LC_MONETARY=English_Canada.utf8
[4] LC_NUMERIC=C                    LC_TIME=English_Canada.utf8    

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

other attached packages:
 [1] raster_3.6-14            sp_1.6-0                 ggplot2_3.4.0            terra_1.7-3             
 [5] SpaDES.tools_1.0.2       SpaDES.core_1.1.1        reproducible_1.2.16.9025 quickPlot_0.1.8.9000    
 [9] LandR_1.1.0.9026         Require_0.2.6.9008      

loaded via a namespace (and not attached):
 [1] nlme_3.1-160        CircStats_0.2-6     amc_0.2.1           fastdigest_0.6-3    bitops_1.0-7       
 [6] fpCompare_0.2.4     sf_1.0-9            bit64_4.0.5         RColorBrewer_1.1-3  httr_1.4.4         
[11] tools_4.2.2         backports_1.4.1     utf8_1.2.2          rgdal_1.6-4         R6_2.5.1           
[16] KernSmooth_2.23-20  rgeos_0.6-1         DBI_1.1.3           colorspace_2.0-3    withr_2.5.0        
[21] tidyselect_1.2.0    fasterize_1.0.4     bit_4.0.5           compiler_4.2.2      cli_3.4.1          
[26] stringfish_0.15.7   scales_1.2.1        checkmate_2.1.0     classInt_0.4-8      pemisc_0.0.4.9008  
[31] proxy_0.4-27        digest_0.6.30       minqa_1.2.5         dismo_1.3-9         base64enc_0.1-3    
[36] pkgconfig_2.0.3     MuMIn_1.47.1        parallelly_1.34.0   lme4_1.1-31         fastmap_1.1.0      
[41] rlang_1.0.6         rstudioapi_0.14     RSQLite_2.2.20      peutils_0.1.0       RApiSerialize_0.1.2
[46] generics_0.1.3      dplyr_1.1.0         car_3.1-1           RCurl_1.98-1.10     magrittr_2.0.3     
[51] Matrix_1.5-1        Rcpp_1.0.9          munsell_0.5.0       fansi_1.0.3         abind_1.4-5        
[56] lifecycle_1.0.3     lobstr_1.1.2        whisker_0.4.1       carData_3.0-5       MASS_7.3-58.1      
[61] grid_4.2.2          blob_1.2.3          parallel_4.2.2      crayon_1.5.2        lattice_0.20-45    
[66] splines_4.2.2       tiler_0.2.6         pillar_1.8.1        igraph_1.3.5        ggpubr_0.5.0       
[71] boot_1.3-28         ggsignif_0.6.4      codetools_0.2-18    stats4_4.2.2        fastmatch_1.1-3    
[76] glue_1.6.2          tidyterra_0.3.1     RcppParallel_5.1.6  data.table_1.14.6   vctrs_0.5.2        
[81] nloptr_2.0.3        gtable_0.3.1        purrr_1.0.1         tidyr_1.3.0         qs_0.25.4          
[86] map_0.0.5           cachem_1.0.6        gridBase_0.4-7      broom_1.0.3         e1071_1.7-13       
[91] rstatix_0.7.2       class_7.3-20        ggspatial_1.1.7     tibble_3.1.8        memoise_2.0.1      
[96] units_0.8-1
@CeresBarros
Copy link
Member Author

I've also tried clearing the cache and setting `options("reproducible.cacheSaveFormat" = "qs"). I see the same error after restarting R and rerunning the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant