| Title: | Helpers for 'OOP' in R |
|---|---|
| Description: | A collection of helper functions designed to help you to better understand object oriented programming in R, particularly using 'S3'. |
| Authors: | Hadley Wickham [aut, cre], Posit Software, PBC [cph, fnd] |
| Maintainer: | Hadley Wickham <[email protected]> |
| License: | GPL-3 |
| Version: | 1.0.1.9000 |
| Built: | 2026-05-07 08:26:10 UTC |
| Source: | https://github.com/r-lib/sloop |
This function figures out whether the input function is a regular/primitive/internal function, a internal/S3/S4 generic, or a S3/S4/RC method. This is function is slightly simplified as it's possible for a method from one class to be a generic for another class, but that seems like such a bad idea that hopefully no one has done it.
ftype(f)ftype(f)
f |
unquoted function name |
a character of vector of length 1 or 2.
ftype(`%in%`) ftype(sum) ftype(t.data.frame) ftype(t.test) # Tricky! ftype(writeLines) ftype(unlist)ftype(`%in%`) ftype(sum) ftype(t.data.frame) ftype(t.test) # Tricky! ftype(writeLines) ftype(unlist)
is_s3_generic() compares name checks for both internal and regular
generics. is_s3_method() builds names of all possible generics for that
function and then checks if any of them actually is a generic.
is_s3_generic(fname, env = parent.frame()) is_s3_method(fname, env = parent.frame())is_s3_generic(fname, env = parent.frame()) is_s3_method(fname, env = parent.frame())
fname |
Name of function as a string. Need name of function because it's impossible to determine whether or not a function is a S3 method based only on its contents. |
env |
Environment to search in. |
is_s3_generic("mean") is_s3_generic("sum") is_s3_generic("[[") is_s3_generic("unlist") is_s3_generic("runif") is_s3_method("t.data.frame") is_s3_method("t.test") # Just tricking! is_s3_method("as.data.frame") is_s3_method("mean.Date")is_s3_generic("mean") is_s3_generic("sum") is_s3_generic("[[") is_s3_generic("unlist") is_s3_generic("runif") is_s3_method("t.data.frame") is_s3_method("t.test") # Just tricking! is_s3_method("as.data.frame") is_s3_method("mean.Date")
Tells you if you're dealing with an base, S3, S4, RC, or R6 object.
otype(x)otype(x)
x |
An object |
otype(1:10) otype(mtcars)otype(1:10) otype(mtcars)
Compared to class(), this always returns the class vector that is
used for dispatch. This is most important for objects where the
class attribute has not been set.
s3_class(x)s3_class(x)
x |
A primitive type |
s3_class(NULL) s3_class(logical()) s3_class(integer()) s3_class(numeric()) s3_class(character()) s3_class(matrix()) s3_class(matrix(1)) s3_class(array()) s3_class(array(1))s3_class(NULL) s3_class(logical()) s3_class(integer()) s3_class(numeric()) s3_class(character()) s3_class(matrix()) s3_class(matrix(1)) s3_class(array()) s3_class(array(1))
s3_dispatch() prints a list of all possible function names that will be
considered for method dispatch. There are four possible states:
=> method exists and is found by UseMethod().
-> method exists and is used by NextMethod().
* method exists but is not used.
Nothing (and greyed out in console): method does not exist.
Learn more at https://adv-r.hadley.nz/s3.html.
s3_dispatch(call, env = parent.frame())s3_dispatch(call, env = parent.frame())
call |
Example call to S3 method |
env |
Environment in which to evaluate call |
x <- Sys.time() s3_dispatch(print(x)) s3_dispatch(is.numeric(x)) s3_dispatch(as.Date(x)) s3_dispatch(sum(x)) # Internal vs. regular generic x1 <- 1 x2 <- structure(2, class = "double") my_length <- function(x) UseMethod("my_length") s3_dispatch(my_length(x1)) s3_dispatch(my_length(x2)) length.double <- function(x) 10 s3_dispatch(length(x1)) s3_dispatch(length(x2))x <- Sys.time() s3_dispatch(print(x)) s3_dispatch(is.numeric(x)) s3_dispatch(as.Date(x)) s3_dispatch(sum(x)) # Internal vs. regular generic x1 <- 1 x2 <- structure(2, class = "double") my_length <- function(x) UseMethod("my_length") s3_dispatch(my_length(x1)) s3_dispatch(my_length(x2)) length.double <- function(x) 10 s3_dispatch(length(x1)) s3_dispatch(length(x2))
Find S3 method from its name
s3_get_method(name)s3_get_method(name)
name |
A string or unquoted symbol |
A function, or an error stating why the method could not be found
s3_get_method(mean.Date) s3_get_method(weighted.mean.Date)s3_get_method(mean.Date) s3_get_method(weighted.mean.Date)
Returns information about all methods belong to a generic or a class.
In S3 and S4, methods belong to a generic, but it is often useful to see what
generics have been provided methods for a given class. These are
wrappers around utils::methods(), which returns a lot of useful information
in an attribute.
s3_methods_class(x) s3_methods_generic(x) s4_methods_class(x) s4_methods_generic(x)s3_methods_class(x) s3_methods_generic(x) s4_methods_class(x) s4_methods_generic(x)
x |
Name of class or generic |
A tibble with columns generic, visible, class, visible,
and source.
s3_methods_class("Date") s3_methods_generic("anova") s4_methods_class("Date") s4_methods_generic("anova")s3_methods_class("Date") s3_methods_generic("anova") s4_methods_class("Date") s4_methods_generic("anova")