EVOLUTION-MANAGER
Edit File: class.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Object Classes</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for class {base}"><tr><td>class {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Object Classes</h2> <h3>Description</h3> <p><span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> 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. </p> <h3>Usage</h3> <pre> class(x) class(x) <- value unclass(x) inherits(x, what, which = FALSE) oldClass(x) oldClass(x) <- value </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> object</p> </td></tr> <tr valign="top"><td><code>what, value</code></td> <td> <p>a character vector naming classes. <code>value</code> can also be <code>NULL</code>.</p> </td></tr> <tr valign="top"><td><code>which</code></td> <td> <p>logical affecting return value: see ‘Details’.</p> </td></tr> </table> <h3>Details</h3> <p>Here, we describe the so called “S3” classes (and methods). For “S4” classes (and methods), see ‘Formal classes’ below. </p> <p>Many <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> objects have a <code>class</code> attribute, a character vector giving the names of the classes from which the object <em>inherits</em>. (Functions <code>oldClass</code> and <code>oldClass<-</code> get and set the attribute, which can also be done directly.) </p> <p>If the object does not have a class attribute, it has an implicit class, notably <code>"matrix"</code>, <code>"array"</code>, <code>"function"</code> or <code>"numeric"</code> or the result of <code><a href="typeof.html">typeof</a>(x)</code> (which is similar to <code><a href="mode.html">mode</a>(x)</code>), but for type <code>"language"</code> and <code><a href="mode.html">mode</a></code> <code>"call"</code>, where the following extra classes exist for the corresponding function <code><a href="call.html">call</a></code>s: <code>if</code>, <code>while</code>, <code>for</code>, <code>=</code>, <code><-</code>, <code>(</code>, <code>{</code>, <code>call</code>. </p> <p>Note that <code><a href="NULL.html">NULL</a></code> objects cannot have attributes (hence not classes) and attempting to assign a class is an error. </p> <p>When a generic function <code>fun</code> is applied to an object with class attribute <code>c("first", "second")</code>, the system searches for a function called <code>fun.first</code> and, if it finds it, applies it to the object. If no such function is found, a function called <code>fun.second</code> is tried. If no class name produces a suitable function, the function <code>fun.default</code> is used (if it exists). If there is no class attribute, the implicit class is tried, then the default method. </p> <p>The function <code>class</code> prints the vector of names of classes an object inherits from. Correspondingly, <code>class<-</code> sets the classes an object inherits from. Assigning <code>NULL</code> removes the class attribute. </p> <p><code>unclass</code> 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.) </p> <p><code>inherits</code> indicates whether its first argument inherits from any of the classes specified in the <code>what</code> argument. If <code>which</code> is <code>TRUE</code> then an integer vector of the same length as <code>what</code> is returned. Each element indicates the position in the <code>class(x)</code> matched by the element of <code>what</code>; zero indicates no match. If <code>which</code> is <code>FALSE</code> then <code>TRUE</code> is returned by <code>inherits</code> if any of the names in <code>what</code> match with any <code>class</code>. </p> <p>All but <code>inherits</code> are <a href="Primitive.html">primitive</a> functions. </p> <h3>Formal classes</h3> <p>An additional mechanism of <em>formal</em> classes, nicknamed “S4”, is available in package <span class="pkg">methods</span> which is attached by default. For objects which have a formal class, its name is returned by <code>class</code> as a character vector of length one and method dispatch can happen on <em>several</em> 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 does <code>inherits</code>. Therefore, S3 methods can be defined for S4 classes. See the ‘<a href="../../methods/html/Introduction.html">Introduction</a>’ and ‘<a href="../../methods/html/Methods_for_S3.html">Methods_for_S3</a>’ help pages for basic information on S4 methods and for the relation between these and S3 methods. </p> <p>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 <code><a href="../../methods/html/as.html">as</a>(object, value)</code> is the way to coerce an object to a particular class. </p> <p>The analogue of <code>inherits</code> for formal classes is <code><a href="../../methods/html/is.html">is</a></code>. The two functions behave consistently with one exception: S4 classes can have conditional inheritance, with an explicit test. In this case, <code>is</code> will test the condition, but <code>inherits</code> ignores all conditional superclasses. </p> <h3>Note</h3> <p>Functions <code>oldClass</code> and <code>oldClass<-</code> behave in the same way as functions of those names in S-PLUS 5/6, <em>but</em> in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> <code><a href="UseMethod.html">UseMethod</a></code> dispatches on the class as returned by <code>class</code> (with some interpolated classes: see the link) rather than <code>oldClass</code>. <em>However</em>, <a href="groupGeneric.html">group generic</a>s dispatch on the <code>oldClass</code> for efficiency, and <a href="InternalMethods.html">internal generic</a>s only dispatch on objects for which <code><a href="is.object.html">is.object</a></code> is true. </p> <p>In older versions of <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span>, assigning a zero-length vector with <code>class</code> removed the class: it is now an error (whereas it still works for <code>oldClass</code>). It is clearer to always assign <code>NULL</code> to remove the class. </p> <h3>See Also</h3> <p><code><a href="UseMethod.html">UseMethod</a></code>, <code><a href="UseMethod.html">NextMethod</a></code>, ‘<a href="groupGeneric.html">group generic</a>’, ‘<a href="InternalMethods.html">internal generic</a>’ </p> <h3>Examples</h3> <pre> 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" </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>