EVOLUTION-MANAGER
Edit File: setOldClass.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: Register Old-Style (S3) Classes and Inheritance</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 setOldClass {methods}"><tr><td>setOldClass {methods}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Register Old-Style (S3) Classes and Inheritance</h2> <h3>Description</h3> <p>Register an old-style (a.k.a. ‘S3’) class as a formally defined class. Simple usage will be of the form: </p> <p><code>setOldClass(Classes)</code> </p> <p>where <code>Classes</code> is the character vector that would be the <code>class</code> attribute of the S3 object. Calls to <code>setOldClass()</code> in the code for a package allow the class to be used as a slot in formal (S4) classes and in signatures for methods (see <a href="Methods_for_S3.html">Methods_for_S3</a>). Formal classes can also contain a registered S3 class (see <a href="S3Part.html">S3Part</a> for details). </p> <p>If the S3 class has a known set of attributes, an equivalent S4 class can be specified by <code>S4Class=</code> in the call to <code>setOldClass()</code>; see the section “Known Attributes”. </p> <h3>Usage</h3> <pre> setOldClass(Classes, prototype, where, test = FALSE, S4Class) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>Classes</code></td> <td> <p>A character vector, giving the names for S3 classes, as they would appear on the right side of an assignment of the <code>class</code> attribute in S3 computations. </p> <p>In addition to S3 classes, an object type or other valid data part can be specified, if the S3 class is known to require its data to be of that form. </p> </td></tr> <tr valign="top"><td><code>S4Class</code></td> <td> <p> optionally, the class definition or the class name of an S4 class. The new class will have all the slots and other properties of this class, plus any S3 inheritance implied by multiple names in the <code>Classes</code> argument. See the section on “S3 classes with known attributes” below. </p> </td></tr> <tr valign="top"><td><code>prototype, where, test</code></td> <td> <p><em>These arguments are currently allowed, but not recommended in typical applications.</em> </p> <p><code>prototype</code>: An optional object to use as the prototype. If the S3 class is not to be <code>VIRTUAL</code> (the default), the use of <code>S4Class=</code> is preferred. </p> <p><code>where</code>: Where to store the class definitions. Should be the default (the package namespace) for normal use in an application package. </p> <p><code>test</code>: flag, if <code>TRUE</code>, arrange to test inheritance explicitly for each object, needed if the S3 class can have a different set of class strings, with the same first string. Such classes are inherently malformed, are rare, and should be avoided. </p> </td></tr> </table> <h3>Details</h3> <p>The name (or each of the names) in <code>Classes</code> will be defined as an S4 class, extending class <code>oldClass</code>, which is the ‘root’ of all old-style classes. S3 classes with multiple names in their class attribute will have a corresponding inheritance as formal classes. See the <code>"mlm"</code> example. </p> <p>S3 classes have no formal definition, and therefore no formally defined slots. If no S4 class is supplied as a model, the class created will be a virtual class. If a virtual class (any virtual class) is used for a slot in another class, then the initializing method for the class needs to put something legal in that slot; otherwise it will be set to <code>NULL</code>. </p> <p>See <a href="Methods_for_S3.html">Methods_for_S3</a> for the details of method dispatch and inheritance with mixed S3 and S4 methods. </p> <p>Some S3 classes cannot be represented as an ordinary combination of S4 classes and superclasses, because objects with the same initial string in the class attribute can have different strings following. Such classes are fortunately rare. They violate the basic idea of object-oriented programming and should be avoided. If you must deal with them, it is still possible to register such classes as S4 classes, but now the inheritance has to be verified for each object, and you must call <code>setOldClass</code> with argument <code>test=TRUE</code>. </p> <h3>Pre-Defined Old Classes</h3> <p>Many of the widely used S3 classes in the standard R distribution come pre-defined for use with S4. These don't need to be explicitly declared in your package (although it does no harm to do so). </p> <p>The list <code>.OldClassesList</code> contains the old-style classes that are defined by the methods package. Each element of the list is a character vector, with multiple strings if inheritance is included. Each element of the list was passed to <code>setOldClass</code> when creating the <span class="pkg">methods</span> package; therefore, these classes can be used in <code><a href="setMethod.html">setMethod</a></code> calls, with the inheritance as implied by the list. </p> <h3>S3 Classes with known attributes</h3> <p>A further specification of an S3 class can be made <em>if</em> the class is guaranteed to have some attributes of known class (where as with slots, “known” means that the attribute is an object of a specified class, or a subclass of that class). </p> <p>In this case, the call to <code>setOldClass()</code> can supply an S4 class definition representing the known structure. Since S4 slots are implemented as attributes (largely for just this reason), the known attributes can be specified in the representation of the S4 class. The usual technique will be to create an S4 class with the desired structure, and then supply the class name or definition as the argument <code>S4Class=</code> to <code>setOldClass()</code>. </p> <p>See the definition of class <code>"ts"</code> in the examples below and the <code>data.frame</code> example in Section 10.2 of the reference. The call to <code><a href="setClass.html">setClass</a></code> to create the S4 class can use the same class name, as here, so long as the call to <code>setOldClass</code> follows in the same package. For clarity it should be the next expression in the same file. </p> <p>In the example, we define <code>"ts"</code> as a vector structure with a numeric slot for <code>"tsp"</code>. The validity of this definition relies on an assertion that all the S3 code for this class is consistent with that definition; specifically, that all <code>"ts"</code> objects will behave as vector structures and will have a numeric <code>"tsp"</code> attribute. We believe this to be true of all the base code in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span>, but as always with S3 classes, no guarantee is possible. </p> <p>The S4 class definition can have virtual superclasses (as in the <code>"ts"</code> case) if the S3 class is asserted to behave consistently with these (in the example, time-series objects are asserted to be consistent with the <a href="StructureClasses.html">structure</a> class). </p> <p>Failures of the S3 class to live up to its asserted behavior will usually go uncorrected, since S3 classes inherently have no definition, and the resulting invalid S4 objects can cause all sorts of grief. Many S3 classes are not candidates for known slots, either because the presence or class of the attributes are not guaranteed (e.g., <code>dimnames</code> in arrays, although these are not even S3 classes), or because the class uses named components of a list rather than attributes (e.g., <code>"lm"</code>). An attribute that is sometimes missing cannot be represented as a slot, not even by pretending that it is present with class <code>"NULL"</code>, because attributes, unlike slots, can not have value <code>NULL</code>. </p> <p>One irregularity that is usually tolerated, however, is to optionally add other attributes to those guaranteed to exist (for example, <code>"terms"</code> in <code>"data.frame"</code> objects returned by <code><a href="../../stats/html/model.frame.html">model.frame</a></code>). Validity checks by <code><a href="validObject.html">validObject</a></code> ignore extra attributes; even if this check is tightened in the future, classes extending S3 classes would likely be exempted because extra attributes are so common. </p> <h3>References</h3> <p>Chambers, John M. (2016) <em>Extending R</em>, Chapman & Hall. (Chapters 9 and 10, particularly Section 10.8) </p> <h3>See Also</h3> <p><code><a href="setClass.html">setClass</a></code>, <code><a href="setMethod.html">setMethod</a></code> </p> <h3>Examples</h3> <pre> require(stats) ## "lm" and "mlm" are predefined; if they were not this would do it: ## Not run: setOldClass(c("mlm", "lm")) ## End(Not run) ## Define a new generic function to compute the residual degrees of freedom setGeneric("dfResidual", function(model) stop(gettextf( "This function only works for fitted model objects, not class %s", class(model)))) setMethod("dfResidual", "lm", function(model)model$df.residual) ## dfResidual will work on mlm objects as well as lm objects myData <- data.frame(time = 1:10, y = (1:10)^.5) myLm <- lm(cbind(y, y^3) ~ time, myData) ## two examples extending S3 class "lm": class "xlm" directly ## and "ylm" indirectly setClass("xlm", slots = c(eps = "numeric"), contains = "lm") setClass("ylm", slots = c(header = "character"), contains = "xlm") ym1 = new("ylm", myLm, header = "Example", eps = 0.) ## for more examples, see ?\link{S3Class}. ## Not run: ## The code in R that defines "ts" as an S4 class setClass("ts", contains = "structure", slots = c(tsp = "numeric"), prototype(NA, tsp = rep(1,3))) # prototype to be a legal S3 time-series ## and now registers it as an S3 class setOldClass("ts", S4Class = "ts", where = envir) ## End(Not run) </pre> <hr /><div style="text-align: center;">[Package <em>methods</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>