R possesses a simple generic function mechanism which can be used for an object-oriented style of programming. Method dispatch takes place based on the class of the first argument to the generic function.
class(x) class(x) <- value unclass(x) inherits(x, what, which = FALSE) isa(x, what) oldClass(x) oldClass(x) <- value .class2(x)
a R object
a character vector naming classes.
logical affecting return value: see ‘Details’.
Here, we describe the so called “S3” classes (and methods). For “S4” classes (and methods), see ‘Formal classes’ below.
Many R objects have a
class attribute, a character vector
giving the names of the classes from which the object inherits.
oldClass<- get and set the
attribute, which can also be done directly.)
If the object does not have a class attribute, it has an implicit
"numeric" or the result of
typeof(x) (which is similar to
but for type
where the following extra classes exist for the corresponding function
Note that for objects
x of an implicit (or an S4) class, when a
(S3) generic function
foo(x) is called, method dispatch may use
more classes than are returned by
class(x), e.g., for a numeric
foo.numeric() method may apply. The exact full
character vector of the classes which
UseMethod() uses, is available as
R version 4.0.0. (This also applies to S4 objects when S3 dispatch is
considered, see below.)
Beware that using
.class2() for other reasons than didactical,
diagnostical or for debugging may rather be a misuse than smart.
NULL objects (of implicit class
"NULL") cannot have
attributes (hence no
class attribute) and attempting to assign a
class is an error.
When a generic function
fun is applied to an object with class
c("first", "second"), the system searches for a
fun.first and, if it finds it, applies it to
the object. If no such function is found, a function called
fun.second is tried. If no class name produces a suitable
function, the function
fun.default is used (if it exists). If
there is no class attribute, the implicit class is tried, then the
class prints the vector of names of classes an
object inherits from. Correspondingly,
class<- sets the
classes an object inherits from. Assigning an empty character vector or
NULL removes the class attribute, as for
direct attribute setting. Whereas it is clearer to explicitly assign
NULL to remove the class, using an empty vector is more natural in
class(x) <- setdiff(class(x), "ts").
unclass returns (a copy of) its argument with its class
attribute removed. (It is not allowed for objects which cannot be
copied, namely environments and external pointers.)
inherits indicates whether its first argument inherits from any
of the classes specified in the
what argument. If
TRUE then an integer vector of the same length as
what is returned. Each element indicates the position in the
class(x) matched by the element of
what; zero indicates
no match. If
inherits if any of the names in
isa tests whether
x is an object of class(es) as given
what by using
x is an S4
object, and otherwise giving
TRUE iff all elements of
class(x) are contained in
isa are primitive functions.
An additional mechanism of formal classes, nicknamed
“S4”, is available in package methods which is attached
by default. For objects which have a formal class, its name is
class as a character vector of length one and
method dispatch can happen on several arguments, instead of
only the first. However, S3 method selection attempts to treat objects
from an S4 class as if they had the appropriate S3 class attribute, as
inherits. Therefore, S3 methods can be defined for S4
classes. See the ‘Introduction’ and ‘Methods_for_S3’
help pages for basic information on S4 methods and for the relation
between these and S3 methods.
The replacement version of the function sets the class to the value
provided. For classes that have a formal definition, directly
replacing the class this way is strongly deprecated. The expression
as(object, value) is the way to coerce an object to a
The analogue of
inherits for formal classes is
is. The two functions behave consistently
with one exception: S4 classes can have conditional
inheritance, with an explicit test. In this case,
test the condition, but
inherits ignores all conditional
oldClass<- behave in the same way
as functions of those names in S-PLUS 5/6, but in R
UseMethod dispatches on the class as returned by
class (with some interpolated classes: see the link) rather
oldClass. However, group generics dispatch
oldClass for efficiency, and internal generics
only dispatch on objects for which
is.object is true.
‘group generic’, ‘internal generic’
x <- 10 class(x) # "numeric" oldClass(x) # NULL inherits(x, "a") #FALSE class(x) <- c("a", "b") inherits(x,"a") #TRUE inherits(x, "a", TRUE) # 1 inherits(x, c("a", "b", "c"), TRUE) # 1 2 0 class( quote(pi) ) # "name" ## regular calls class( quote(sin(pi*x)) ) # "call" ## special calls class( quote(x <- 1) ) # "<-" class( quote((1 < 2)) ) # "(" class( quote( if(8<3) pi ) ) # "if" .class2(pi) # "double" "numeric" .class2(matrix(1:6, 2,3)) # "matrix" "array" "integer" "numeric"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.