EVOLUTION-MANAGER
Edit File: s3_register.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Register a method for a suggested dependency</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for s3_register {vctrs}"><tr><td>s3_register {vctrs}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Register a method for a suggested dependency</h2> <h3>Description</h3> <p>Generally, the recommend way to register an S3 method is to use the <code>S3Method()</code> namespace directive (often generated automatically by the <code style="white-space: pre;">@export</code> roxygen2 tag). However, this technique requires that the generic be in an imported package, and sometimes you want to suggest a package, and only provide a method when that package is loaded. <code>s3_register()</code> can be called from your package's <code>.onLoad()</code> to dynamically register a method only if the generic's package is loaded. </p> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>generic</code></td> <td> <p>Name of the generic in the form <code>pkg::generic</code>.</p> </td></tr> <tr valign="top"><td><code>class</code></td> <td> <p>Name of the class</p> </td></tr> <tr valign="top"><td><code>method</code></td> <td> <p>Optionally, the implementation of the method. By default, this will be found by looking for a function called <code>generic.class</code> in the package environment. </p> <p>Note that providing <code>method</code> can be dangerous if you use devtools. When the namespace of the method is reloaded by <code>devtools::load_all()</code>, the function will keep inheriting from the old namespace. This might cause crashes because of dangling <code>.Call()</code> pointers.</p> </td></tr> </table> <h3>Details</h3> <p>For R 3.5.0 and later, <code>s3_register()</code> is also useful when demonstrating class creation in a vignette, since method lookup no longer always involves the lexical scope. For R 3.6.0 and later, you can achieve a similar effect by using "delayed method registration", i.e. placing the following in your <code>NAMESPACE</code> file: </p> <div class="sourceCode"><pre>if (getRversion() >= "3.6.0") { S3method(package::generic, class) } </pre></div> <h3>Usage in other packages</h3> <p>To avoid taking a dependency on vctrs, you copy the source of <a href="https://github.com/r-lib/vctrs/blob/main/R/register-s3.R"><code>s3_register()</code></a> into your own package. It is licensed under the permissive <a href="https://choosealicense.com/licenses/unlicense/">unlicense</a> to make it crystal clear that we're happy for you to do this. There's no need to include the license or even credit us when using this function. </p> <h3>Examples</h3> <pre> # A typical use case is to dynamically register tibble/pillar methods # for your class. That way you avoid creating a hard dependency on packages # that are not essential, while still providing finer control over # printing when they are used. .onLoad <- function(...) { s3_register("pillar::pillar_shaft", "vctrs_vctr") s3_register("tibble::type_sum", "vctrs_vctr") } </pre> <hr /><div style="text-align: center;">[Package <em>vctrs</em> version 0.5.0 <a href="00Index.html">Index</a>]</div> </body></html>