ID EN
Vector & List

class

R Base 3.6.2

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.

Syntax

R
class(x)
class(x) <- value
unclass(x)
inherits(x, what, which = FALSE)<p></p><p>oldClass(x)
oldClass(x) &lt;- value</p>

Arguments

Parameter Description
x a R object
what, value a character vector naming classes. value can also be NULL.
which logical affecting return value: see ‘Details’.

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. (Functions oldClass and 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 class, notably "matrix", "array", "function" or "numeric" or the result of typeof(x) (which is similar to mode(x)), but for type "language" and mode "call", where the following extra classes exist for the corresponding function calls: if, while, for, =, <-, (, {, call. Note that NULL objects cannot have attributes (hence not classes) and attempting to assign a class is an error. When a generic

Examples

Example
R
# NOT RUN {
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"
# }

See Also

UseMethod NextMethod ‘group generic’ ‘internal generic’