When using S3 methods from dependent packages, how do I ensure that a package loads (and attaches?) all dependent packages when called using the double colon (::) operator without a prior library()
call?
I am developing a package package3
that uses package1
and package2
. package1
defines an S3 method funky
and package2
defines methods for additional types. If I use the funky
method in my own package, package3
, then R cannot find the relevant S3 method, even though package3
imports both package1
and package2
.
The actual problems occurs with the officer and mschart project, but it is generic and I have created a reusable example using 3 separate packages in RStudio
Context
package1
Defines funky(arg)
and funky.character(arg)
. Both methods are exported
package2
Defines funky.numeric(arg)
. Imports package1
However, Roxygen does not generate a S3method
in NAMESPACE
package3
Defines hello()
which calls funky(100)
(i.e the numeric method should be called). Imports package1
and package2
(I've also tried with depends),
Reproduce the problem
package3::hello()
generates an error regarding the missing S3 method
Workaround
library(package3)
package3::hello()
However, because the package will be used internally within our company I can't expect everyone to know that library()
has to be called first
Any ideas on how I can ensure that all relevant S3 methods are available even when using the double colon (::) operator?
Many thanks in advance
Jonas