How can I import an S3 method from an R
package without depending on the package when the generic function of the method is not defined in that package?
More specifically, my btergm
package (on CRAN and GitHub) imports several functions from the ergm
package. It also uses the formula
method (defined in ergm
) of the simulate
generic function (defined in the methods
package). I would like to import this method from ergm
. How do I do that?
I have read elsewhere that I could just add a dependency to the ergm
package. But I do not want to move ergm
from Imports:
to Depends:
in the description because the btergm
package defines its own gof
function, while a function with that name is also present in the ergm
package. This would cause a warning that my package overwrites the gof
function after loading ergm
, and CRAN does not like warnings.
It kind of works without the import at the moment. However, the lme4
package also defines a formula
method for the simulate
generic function. If somebody loads lme4
after loading ergm
, the wrong method is picked by my code. Hence the need for a proper import.
The current setup in the description file of the btergm
package:
Imports: stats4, utils, methods, graphics, network (>= 1.13.0), sna (>= 2.3.2), ergm (>= 3.10.0), parallel, Matrix (>= 1.2.2), boot (>= 1.3.17), coda (>= 0.18.1), stats, ROCR (>= 1.0.7), speedglm (>= 0.3.1), igraph (>= 0.7.1), RSiena (>= 1.0.12.232), statnet.common (>= 4.2.0)
Suggests:
fastglm (>= 0.0.1),
testthat
Depends: R (>= 3.5), xergm.common (>= 1.7.7), ggplot2 (>= 2.0.0)
And the relevant part from the namespace file:
import("methods")
...
importFrom("ergm", "ergmMPLE")
importFrom("ergm", "control.simulate.formula")
importFrom("ergm", "remove.offset.formula")
importFrom("ergm", "ergm.getnetwork")
importFrom("ergm", "ergm.getmodel")
importFrom("ergm", "ergm.Cprepare")
importFrom("ergm", "ergm.design")
importFrom("ergm", "ergm.pl")
importFrom("ergm", "control.ergm")
importFrom("ergm", "ergm.getglobalstats")
importFrom("ergm", "ergm.geodistdist")
importFrom("ergm", "ergm")
importFrom("ergm", "mcmc.diagnostics")
In the ergm
package, the namespace export looks as follows:
S3method(simulate,formula)
How do I import this now? Is it importFrom("ergm", "simulate")
or importFrom("ergm", "simulate.formula")
or importFrom("ergm", "formula-method")
or something completely different? The Writing R Extensions section on specifying imports does not say anything about this.