Pakillo / rSDM

Species Distribution Modelling in R
https://pakillo.github.io/rSDM/
7 stars 1 forks source link

BUG: point_in_cell throws Error in missing[, "value"] : subscript out of bounds #6

Closed apsteward closed 4 months ago

apsteward commented 4 months ago

Hi there, thanks for creating this package, the points2nearestcell function is exactly what I was looking for. However, when I run the example from the documentation I get Error in missing[, "value"] : subscript out of bounds in the terminal.

Tracing it back I believe the issue is with missing[, "value"] in the point_in_cell function, which of course is used by the points2nearestcell function. When I hashed out this line everything worked OK (I wasn't quite sure what its purpose is anyway), but maybe omitting it will cause other issues? What do you think?

Cheers!

point_in_cell <- function(locs = NULL, ras = NULL, layer = 1) {

  if (!inherits(locs, "sf")) {
    if (!inherits(locs, "SpatVector")) {
      stop("locs must be a sf or SpatVector object. You may find 'locs2sf()' or 'locs2vect()' useful.")
    }
  }

  stopifnot(is.numeric(layer))
  stopifnot(layer >= 1 && layer <= terra::nlyr(ras))

  locs.terra <- terra::vect(locs)

  if (!isTRUE(terra::same.crs(locs.terra, ras))){
    stop("Coordinate data and raster object must have the same projection.")

  }

  ## Get NA cells
  rasvals <- terra::extract(ras, locs.terra, layer = layer, raw = TRUE, ID = FALSE)
  missing <- is.na(rasvals)
  missing[, "value"]  # TRIGGERS ERROR

}
Pakillo commented 4 months ago

Hi @apsteward

Thanks for the report. Could you paste here the output of calling sessionInfo()? I'm afraid I can't reproduce the problem

Thanks

apsteward commented 4 months ago

Thanks for the rapid response @Pakillo. I updated to the latest version of R with a fresh install and this is now working (I didn't realise but R Studio hadn't automatically been using the latest R versions after I downloaded them, so I was pretty out of date!).

We can probably close this but for your information this is the sessionInfo() output when it wasn't working:

R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

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

time zone: Europe/London
tzcode source: internal

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

other attached packages:
[1] rSDM_0.4.0   terra_1.7-29

loaded via a namespace (and not attached):
 [1] gtable_0.3.5       dplyr_1.1.2        compiler_4.3.0     tidyselect_1.2.1   Rcpp_1.0.10       
 [6] leaflet_2.2.2      scales_1.3.0       fastmap_1.2.0      lattice_0.21-8     ggplot2_3.5.1     
[11] R6_2.5.1           shinyjs_2.1.0      generics_0.1.3     classInt_0.4-9     sf_1.0-12         
[16] htmlwidgets_1.6.4  tibble_3.2.1       units_0.8-2        munsell_0.5.1      DBI_1.2.3         
[21] pillar_1.9.0       rlang_1.1.1        utf8_1.2.3         sp_1.6-0           cli_3.6.1         
[26] magrittr_2.0.3     class_7.3-21       crosstalk_1.2.1    digest_0.6.36      grid_4.3.0        
[31] rstudioapi_0.14    lifecycle_1.0.4    vctrs_0.6.2        KernSmooth_2.23-20 proxy_0.4-27      
[36] glue_1.6.2         raster_3.6-20      codetools_0.2-19   colorspace_2.1-0   fansi_1.0.4       
[41] e1071_1.7-13       tools_4.3.0        pkgconfig_2.0.3    htmltools_0.5.8.1 
Pakillo commented 4 months ago

Many thanks Alistair. Glad to hear it works now. The problem was probably related to using some oldish version of {terra} package. Will try to specify a minimum version of it if I find out which one it is

Cheers