View source: R/method-introspect.R
method | R Documentation |
method()
takes a generic and class signature and performs method dispatch
to find the corresponding method implementation. This is rarely needed
because you'll usually rely on the the generic to do dispatch for you (via
S7_dispatch()
). However, this introspection is useful if you want to see
the implementation of a specific method.
method(generic, class = NULL, object = NULL)
generic |
A generic function, i.e. an S7 generic, an external generic, an S3 generic, or an S4 generic. |
class , object |
Perform introspection either with a |
Either a function with class S7_method
or an error if no
matching method is found.
method_explain()
to explain why a specific method was picked.
# Create a generic and register some methods
bizarro <- new_generic("bizarro", "x")
method(bizarro, class_numeric) <- function(x) rev(x)
method(bizarro, class_factor) <- function(x) {
levels(x) <- rev(levels(x))
x
}
# Printing the generic shows the registered method
bizarro
# And you can use method() to inspect specific implementations
method(bizarro, class = class_integer)
method(bizarro, object = 1)
method(bizarro, class = class_factor)
# errors if method not found
try(method(bizarro, class = class_data.frame))
try(method(bizarro, object = "x"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.