Skip to content
Prev 1777 / 12125 Next

[R-pkg-devel] Generic method redefinition and dispatch

Hello,

Package 'ggplot2' defines `ggplot()` as an S3 generic.

My package 'ggspectra' defines some specializations of ggplot() for 
additional classes, which work as expected together with all packages 
extending 'ggplot2' that I have tested, except for 'ggtern'.

Package 'ggtern' imports 'ggplot2' and then redefines the ggplot() 
generic with exactly the same code as in 'ggplot2' itself.

If one attempts to use my package and 'ggtern' in the same session, 
dispatch fails for the ggplot() specializations defined in my package, 
unless an explicit call to ggplot2::ggpplot() is used.

I do not understand why the generic needs to be redefined in 'ggtern', 
and although I have raised an issue for 'ggtern' last November, the just 
released version of 'ggtern' retains the redefinition of the generic.

So, I have some questions:

1) Can I do anything in my own package to ensure that method dispatch 
works correctly even after 'ggtern' is attached?

2) Is it good practice for a package to redefine a generic from an 
imported package?

3) Is it possible to redefine a generic, without disturbing the dispatch 
of specializations already known to R?

Thanks in advance for any insights,

Pedro.