title: Import an S3 method from a package that does not define the generic function
body: <p>How can I import an S3 method from an <code>R</code> package without <em>depending</em> on the package when the generic function of the method is not defined in that package?</p>
  
  <p>More specifically, my <code>btergm</code> package (on <a href="https://cran.r-project.org/package=btergm" rel="nofollow noreferrer">CRAN</a> and <a href="https://github.com/leifeld/btergm" rel="nofollow noreferrer">GitHub</a>) imports several functions from the <code>ergm</code> package. It also uses the <code>formula</code> method (defined in <code>ergm</code>) of the <code>simulate</code> generic function (defined in the <code>methods</code> package). I would like to import this method from <code>ergm</code>. How do I do that?</p>
  
  <p>I have read elsewhere that I could just add a dependency to the <code>ergm</code> package. But I do not want to move <code>ergm</code> from <code>Imports:</code> to <code>Depends:</code> in the description because the <code>btergm</code> package defines its own <code>gof</code> function, while a function with that name is also present in the <code>ergm</code> package. This would cause a warning that my package overwrites the <code>gof</code> function after loading <code>ergm</code>, and CRAN does not like warnings.</p>
  
  <p>It kind of works without the import at the moment. However, the <code>lme4</code> package also defines a <code>formula</code> method for the <code>simulate</code> generic function. If somebody loads <code>lme4</code> after loading <code>ergm</code>, the wrong method is picked by my code. Hence the need for a proper import.</p>
  
  <p>The current setup in the description file of the <code>btergm</code> package:</p>
  
  <pre><code>Imports: stats4, utils, methods, graphics, network (&gt;= 1.13.0), sna (&gt;= 2.3.2), ergm (&gt;= 3.10.0), parallel, Matrix (&gt;= 1.2.2), boot (&gt;= 1.3.17), coda (&gt;= 0.18.1), stats, ROCR (&gt;= 1.0.7), speedglm (&gt;= 0.3.1), igraph (&gt;= 0.7.1), RSiena (&gt;= 1.0.12.232), statnet.common (&gt;= 4.2.0)
  Suggests: 
      fastglm (&gt;= 0.0.1),
      testthat
  Depends: R (&gt;= 3.5), xergm.common (&gt;= 1.7.7), ggplot2 (&gt;= 2.0.0)
  </code></pre>
  
  <p>And the relevant part from the namespace file:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>In the <code>ergm</code> package, the namespace export looks as follows:</p>
  
  <pre><code>S3method(simulate,formula)
  </code></pre>
  
  <p>How do I import this now? Is it <code>importFrom("ergm", "simulate")</code> or <code>importFrom("ergm", "simulate.formula")</code> or <code>importFrom("ergm", "formula-method")</code> or something completely different? The <a href="https://cran.r-project.org/doc/manuals/R-exts.html#Specifying-imports-and-exports" rel="nofollow noreferrer">Writing R Extensions</a> section on specifying imports does not say anything about this.</p>