vctrs (version 0.0.0.9000)

vec_method_register: Register a method for a suggested dependency

Description

Generally, the recommend way to register an S3 method is to use the S3Method() namespace directive (often generated automatically be the @export 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. vec_method_register() should be called from your package's .onLoad() to dynamically register a method only if the generic's package is loaded.

Usage

vec_method_register(generic, class, method = NULL)

Arguments

generic

Name of the generic in the form pkg::generic.

class

Name of the class

method

Optionally, the implementation of the method. By default, this will be found by looking for a function called generic.class in the package environment.

Details

vec_method_register() is also useful when demonstrating class creation in a vignette, since as of R 3.5.0, method lookup no longer always involves the lexical scope.

As of R 3.6.0, a similar effect can be accomplished by using "delayed method registration", by placing the following in your NAMESPACE file:

if (getRversion() >= "3.6.0") {
  S3method(package::generic, class)
}

Examples

Run this code
# NOT RUN {
# A typical use case is to dynamically register tibble/pillar methods
# for your class. That way you avoid creating a hard depedency on packages
# that are not essential, while still providing finer control over
# printing when they are used.

.onLoad <- function(...) {
  vec_method_register("pillar::pillar_shaft", "vctrs_vctr")
  vec_method_register("tibble::type_sum", "vctrs_vctr")
}
# }

Run the code above in your browser using DataCamp Workspace