r-lib/pkgload

S3 methods not dispatached after load_all()

weiyaw opened this issue · 2 comments

Suppose that you have a class myclass with a plot function in a package

plot.myclass <- function(x) {
  print('Hello!')
}

Normally after load_all, running plot(structure('a', class = 'myclass')) will print "Hello!"

plot(structure('a', class = 'myclass'))
[1] "Hello!"

This works on R 3.6.2 with the following session.

R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

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

other attached packages:
[1] devtools_2.2.1 usethis_1.6.1 

loaded via a namespace (and not attached):
 [1] prettyunits_1.0.2 ps_1.3.0          fansi_0.4.1       rprojroot_1.3-2  
 [5] withr_2.1.2       digest_0.6.25     crayon_1.3.4      assertthat_0.2.1 
 [9] R6_2.4.1          backports_1.1.5   magrittr_1.5      rlang_0.4.6      
[13] cli_2.0.2         remotes_2.1.0     fs_1.4.1          testthat_2.3.1   
[17] callr_3.4.0       ellipsis_0.3.1    desc_1.2.0        tools_3.6.2      
[21] glue_1.4.1        pkgload_1.0.2     compiler_3.6.2    processx_3.4.1   
[25] pkgbuild_1.0.6    sessioninfo_1.1.1 memoise_1.1.0    

However, since upgrading to R 4.0.2, plot(structure('a', class = 'myclass')) will dispatch the default plot method unless plot.myclass is called explicitly:

plot(structure('a', class = 'myclass'))
Error in plot.window(...) : need finite 'ylim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

The problematic session is

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

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

other attached packages:
[1] devtools_2.3.0 usethis_1.6.1 

loaded via a namespace (and not attached):
 [1] ps_1.3.3          fansi_0.4.1       prettyunits_1.1.1 rprojroot_1.3-2  
 [5] withr_2.2.0       digest_0.6.25     crayon_1.3.4      assertthat_0.2.1 
 [9] R6_2.4.1          backports_1.1.8   magrittr_1.5      rlang_0.4.6      
[13] cli_2.0.2         remotes_2.1.1     fs_1.4.1          testthat_2.3.2   
[17] callr_3.4.3       ellipsis_0.3.1    desc_1.2.0        tools_4.0.2      
[21] glue_1.4.1        compiler_4.0.2    pkgload_1.1.0     processx_3.4.2   
[25] pkgbuild_1.0.8    sessioninfo_1.1.1 memoise_1.1.0    

Can someone please take a look at this issue? Thanks.

The way S3 registration works has changed in more recent versions of R.

You need to explicitly register S3 methods in packages, this has nothing really to do with pkgload.

If you are using roxygen2 then using #' @export on the S3 method and re-documenting your package should fix this.

Thanks for your feedback. I wasn't really sure where the problem was so I thought it might be due to pkgload.

Should have posted this on stack overflow.