OpenDendro / dplR

This is the dev site for the dplR package in R
38 stars 14 forks source link

Conflict with corr.rwl.seg() class "crs" and sf package class "crs" (coordinate reference system) #19

Closed ctmaher closed 3 weeks ago

ctmaher commented 1 year ago

Hi Andy and dplR crew,

I came across an issue using corr.rwl.seg() with the sf ("simple features") package loaded.

I got this error when I ran corr.rwl.seg(): "Coordinate Reference System: Error: invalid crs object: wrong length"

I think it is related to this line near the end of the corr.rwl.seg() code: class(res) <- c("list", "crs")

In the sf package, class "crs" is a coordinate reference system (like class "CRS" in the sp package).

Here's a simple way to test this:

library(dplR) data("ca533") corr.rwl.seg(ca533) # works fine

load the sf package

library(sf) corr.rwl.seg(ca533) # throws the error

Cheers,

Colin

AndyBunn commented 1 year ago

I do not get that method conflict under recent versions of sf or dplR. Versions sf_1.0-12 and dplR_1.7.4 respectively and R v4.2.2. I.e., this works fine.

library(dplR)
library(sf)

data(co021)
foo <- corr.rwl.seg(co021, make.plot = FALSE)
plot(foo)

Can you confirm?

ctmaher commented 1 year ago

Sorry for the delay,

I am running dplR 1.7.5, sf 1.0-14, & R 4.3.1

I still get the same error. Here's the thing though, the function appears to work anyway. At least I get a plot. When I assign the output to a vector, as you have done above, I don't get the error. The vector contains the output and I'm able to plot it too. The error message doesn't seem to mean anything now.

MarkoKazimirovic commented 1 year ago

It still throws the error though it returns the plot and results just normally, but breaks the further code block evaluation. Strange behaviour, especially because I didn't even have an attached sf. I didn't have time to pass through the code carefully, just changed the class name and generic and it worked out just fine, so that's the easiest conflict solution. In addition, dot arguments should be wrapped somewhat and passed to plot generic, currently, that is not working as it is written in docs.

ctmaher commented 1 year ago

Sounds good, thanks for addressing this!

AndyBunn commented 11 months ago

What platform? I still can't replicate this.

library(dplR)
library(sf)

data(co021)
foo <- corr.rwl.seg(co021, make.plot = FALSE)
plot(foo)
MarkoKazimirovic commented 11 months ago

Unfortunately, I neither couldn't reproduce it anymore. It seemed a subtle error, likely appearing in particular circumstances when certain packages in a specific order are attached to the search path or loaded via namespace, making dispatch find some other plot.crs. (Then how do I get the plot if another method is called?) I tried to rerun scripts from the beginning, but it didn't give me an idea of what happened. Usually, I don't attach packages, but in this case, magrritr, raster, ggplot2 and dplR were added explicitly (in that order). That doesn't mean I didn't attach some other package directly from the console, sometimes I do that when experimenting as I was in that first script for making the maps. dplR was called in the second script, and then an error appeared.

Here is the season info, but it does not mean much.

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

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    

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

other attached packages:
[1] dplR_1.7.5     ggplot2_3.4.4  raster_3.6-26  sp_2.1-1       magrittr_2.0.3

loaded via a namespace (and not attached):
 [1] rgl_1.2.1          Rcpp_1.0.11        lattice_0.20-45    prettyunits_1.2.0  png_0.1-8         
 [6] class_7.3-20       digest_0.6.33      foreach_1.5.2      utf8_1.2.4         plyr_1.8.9        
[11] R6_2.5.1           signal_0.7-7       e1071_1.7-13       pillar_1.9.0       rlang_1.1.2       
[16] progress_1.2.2     rstudioapi_0.15.0  extrafontdb_1.0    magick_2.8.1       R.utils_2.12.2    
[21] R.oo_1.25.0        Matrix_1.6-3       extrafont_0.19     stringr_1.5.1      htmlwidgets_1.6.2 
[26] munsell_0.5.0      proxy_0.4-27       compiler_4.2.2     xfun_0.41          pkgconfig_2.0.3   
[31] base64enc_0.1-3    htmltools_0.5.7    tidyselect_1.2.0   tibble_3.2.1       codetools_0.2-18  
[36] matrixStats_1.1.0  XML_3.99-0.15      fansi_1.0.5        crayon_1.5.2       dplyr_1.1.4       
[41] rayshader_0.35.7   withr_2.5.2        sf_1.0-14          MASS_7.3-58.1      R.methodsS3_1.8.2 
[46] grid_4.2.2         jsonlite_1.8.7     Rttf2pt1_1.3.12    gtable_0.3.4       lifecycle_1.0.4   
[51] DBI_1.1.3          giscoR_0.4.0       units_0.8-4        scales_1.2.1       KernSmooth_2.23-20
[56] cli_3.6.1          stringi_1.8.1      renv_1.0.3         doParallel_1.0.17  generics_0.1.3    
[61] vctrs_0.6.4        boot_1.3-28        iterators_1.0.14   tools_4.2.2        glue_1.6.2        
[66] hms_1.1.3          parallel_4.2.2     fastmap_1.1.1      colorspace_2.1-0   terra_1.7-55      
[71] classInt_0.4-10    knitr_1.45    

The scripts are for generic use, so I am sure this will appear again, and then I will capture more info. Till then, the error is not critical. You should add dots handling on the TODO list for the next version, now they are just vanishing.