rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
543 stars 90 forks source link

terra::plot error - "no slot of name "cpp" for this object of class "SpatRaster" #1577

Closed nafreymueller closed 3 months ago

nafreymueller commented 3 months ago

terra::plot() is throwing an error for any kind of plot.

When I try and plot even a basic raster:

library(terra)
# crs
ps.crs_km <- "+proj=stere +lat_0=90 +lat_ts=71 +datum=WGS84 +units=km +no_defs +ellps=WGS84"
# example raster
arctic.rast <- rast(xmin=-4500, xmax=4500, ymin=-4500, ymax=4500, crs=ps.crs_km, resolution=100 )
arctic.rast
values(arctic.rast) <- 1:ncell(arctic.rast)
plot(arctic.rast)

I get this error:

Error in .local(x, ...) : 
  no slot of name "cpp" for this object of class "SpatRaster"

I've tried updating R and RStudio, re-installing Rcpp, and terra, and none of it seems to work.

Any ideas?

sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.6.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Australia/Adelaide
tzcode source: internal

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

other attached packages:
 [1] INLA_24.05.01-1         sp_2.1-4                Matrix_1.7-0           
 [4] stringr_1.5.1           ggpubr_0.6.0            beepr_2.0              
 [7] terrainr_0.7.5          rnaturalearthdata_1.0.0 rnaturalearth_1.0.1    
[10] stars_0.6-6             abind_1.4-5             sf_1.0-16              
[13] terra_1.7-81            ggOceanMaps_2.2.0       ggplot2_3.5.1          
[16] dplyr_1.1.4             data.table_1.15.4      

loaded via a namespace (and not attached):
  [1] RColorBrewer_1.1-3     audio_0.1-11           rstudioapi_0.16.0     
  [4] jsonlite_1.8.8         reproj_0.7.0           magrittr_2.0.3        
  [7] spatstat.utils_3.0-5   farver_2.1.2           vctrs_0.6.5           
 [10] spatstat.explore_3.3-1 rstatix_0.7.2          broom_1.0.6           
 [13] raster_3.6-26          cellranger_1.1.0       proj4_1.0-14          
 [16] aniMotum_1.2-06        KernSmooth_2.23-24     sandwich_3.1-0        
 [19] rootSolve_1.8.2.4      zoo_1.8-12             lubridate_1.9.3       
 [22] TMB_1.9.14             lifecycle_1.0.4        pkgconfig_2.0.3       
 [25] R6_2.5.1               Exact_3.3              colorspace_2.1-0      
 [28] patchwork_1.2.0        CircStats_0.2-6        tensor_1.5            
 [31] ellipse_0.5.0          labeling_0.4.3         lwgeom_0.2-14         
 [34] fansi_1.0.6            spatstat.sparse_3.1-0  timechange_0.3.0      
 [37] httr_1.4.7             polyclip_1.10-7        compiler_4.4.1        
 [40] proxy_0.4-27           intervals_0.15.4       withr_3.0.0           
 [43] backports_1.5.0        carData_3.0-5          viridis_0.6.5         
 [46] DBI_1.2.3              ggsignif_0.6.4         MASS_7.3-61           
 [49] traipse_0.3.0          classInt_0.4-10        wesanderson_0.3.7     
 [52] gld_2.6.6              tools_4.4.1            units_0.8-5           
 [55] goftest_1.2-3          glue_1.7.0             nlme_3.1-165          
 [58] grid_4.4.1             generics_0.1.3         gtable_0.3.5          
 [61] spatstat.data_3.1-2    class_7.3-22           tidyr_1.3.1           
 [64] lmom_3.0               car_3.1-2              utf8_1.2.4            
 [67] spatstat.geom_3.3-2    pillar_1.9.0           splines_4.4.1         
 [70] crsmeta_0.3.0          gmm_1.8                lattice_0.22-6        
 [73] deldir_2.0-4           tidyselect_1.2.1       pathroutr_0.2.1       
 [76] gridExtra_2.3          unifir_0.2.4           stats4_4.4.1          
 [79] expm_0.999-9           stringi_1.8.4          boot_1.3-30           
 [82] codetools_0.2-20       tibble_3.2.1           cli_3.6.3             
 [85] DescTools_0.99.54      munsell_0.5.1          Rcpp_1.0.13           
 [88] readxl_1.4.3           spatstat.random_3.3-1  parallel_4.4.1        
 [91] spatstat.univar_3.0-0  MatrixModels_0.5-3     fmesher_0.1.7         
 [94] viridisLite_0.4.2      mvtnorm_1.2-5          trip_1.10.0           
 [97] scales_1.3.0           xts_0.14.0             tmvtnorm_1.6          
[100] e1071_1.7-14           purrr_1.0.2            rlang_1.1.4   
rhijmans commented 3 months ago

You do not show the version of "terra" you are using (what is printed when you do library(terra) I am not sure if we can always trust the sessionInfo). But your example work fine for me with the CRAN and development version.

library(terra)
#terra 1.7.78

The type of problem you are having is almost always because you use RStudio. It can also be related to loading an old session. To work around that, open basic R and make sure the session is empty. If you get a message about loading a previous session, it is not. Also see ls() (nothing should be printed). You can get rid of previous sessions by running unlink(".RData") . If that all looks good, try install.packages("terra") and run your code (all outside of RStudio).

nafreymueller commented 3 months ago

Thanks for getting back to me so promptly! Yes I was using terra 1.7.78. I was able to run everything in base R without issue.

It didn't seem to be related to any previous sessions being loaded (after running unlink(".RData") and rm(list=ls()). I weirdly have been getting the "One or more of the packages to be updated are currently loaded. Restarting R prior to install is highly recommended." notification when I try to update terra, even in a restarted RStudio session. So this does appear to be a RStudio-specific issue. Thanks!

rhijmans commented 3 months ago

There seem to multiple issues with RStudio with updating packages. One reason seems to be that it tries to load packages used in scripts as soon as you open the program. What you may need to do is to close all files, especially Rmd files if you have any, and then exit RStudio and start it again.