Rev 71366 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/Classes.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2016 R Core Team% Distributed under GPL 2 or later\name{Classes_Details}\alias{Classes_Details}\alias{__ClassMetaData}\alias{.environment-class}\alias{.externalptr-class}\alias{.name-class}\alias{.NULL-class}\title{Class Definitions}\description{Class definitions are objects that contain the formal definition of aclass of \R objects, usually referred to as an S4 class, todistinguish them from the informal S3 classes.This document gives an overview of S4 classes; fordetails of the class representation objects, see help for the class\code{\linkS4class{classRepresentation}}.}\section{Metadata Information}{When a class is defined, an object is stored that contains theinformation about that class. The object, known as the\emph{metadata} defining the class, is not stored under the name ofthe class (to allow programmers to write generating functions ofthat name), but under a specially constructed name.To examine the class definition, call \code{\link{getClass}}. Theinformation in the metadata object includes:\describe{\item{Slots:}{The data contained in an object from an S4 class is defined bythe \emph{slots} in the class definition.Each slot in an object is a component of the object;like components (that is, elements) of alist, these may be extracted and set, using thefunction \code{\link{slot}()} or more often the operator\code{"\link{@}"}. However, theydiffer from list components in important ways.First, slots can only be referred to by name, not by position,and there is no partial matching of names as with list elements.All the objects from a particular class have the same set of slotnames; specifically, the slot names that are contained in theclass definition. Each slot in each object always is an objectof theclass specified for this slot in the definition of the current class.The word \dQuote{is} corresponds to the \R function of the samename (\code{\link{is}}), meaning that the class of the object inthe slot must be the same as the class specified in thedefinition, or some class that extends the one in thedefinition (a \emph{subclass}).A special slot name, \code{.Data}, stands for the\sQuote{data part} of the object. An object from a class with adata part is defined by specifying that the class contains oneof the \R object types or one of the special pseudo-classes,\code{matrix} or \code{array}, usually because the definition ofthe class, or of one of its superclasses, has included the typeor pseudo-class in its \code{contains} argument. A secondspecial slot name, \code{.xData}, is used to enable inheritancefrom abnormal types such as \code{"environment"}See the section on inheriting from non-S4 classesfor details on the representation andfor the behavior of S3 methods with objects from these classes.Some slot names correspond to attributes used in old-style S3objects and in \R objects without an explicit class, forexample, the \code{names} attribute. If you define a class forwhich that attribute will be set, such as a subclass of namedvectors, you should include \code{"names"} as a slot. See thedefinition of class \code{"namedList"} for an example. Using the\code{names()} assignment to set such names will generate awarning if there is no names slot and an error if the object inquestion is not a vector type. A slot called \code{"names"} canbe used anywhere, but only if it is assigned as a slot, not viathe default \code{names()} assignment.}\item{Superclasses:}{The definition of a class includes the \emph{superclasses} ---theclasses that this class extends. Aclass \code{Fancy}, say, extends a class \code{Simple} if anobject from the \code{Fancy} class has all the capabilities ofthe \code{Simple} class (and probably some more as well). Inparticular, and very usefully, any method defined to work for a\code{Simple} object can be applied to a \code{Fancy} object aswell.This relationship is expressed equivalently by saying that\code{Simple} is a superclass of \code{Fancy}, or that\code{Fancy} is a subclass of \code{Simple}.The direct superclasses of a class are those superclassesexplicitly defined. Direct superclasses can be defined inthree ways. Most commonly, the superclasses are listed in the\code{contains=} argument in the call to \code{\link{setClass}}that creates the subclass. In this case the subclass willcontain all the slots of the superclass, and the relationbetween the class is called \emph{simple}, as it in fact is.Superclasses can also be definedexplicitly by a call to \code{\link{setIs}}; in this case, therelation requires methods to be specified to go from subclass tosuperclass. Thirdly, a class union is a superclass of all themembers of the union. In this case too the relation is simple,but notice that the relation is defined when the superclass iscreated, not when the subclass is created as with the\code{contains=} mechanism.The definition of a superclass will also potentially containits own direct superclasses. These are considered (and shown) assuperclasses at distance 2 from the original class; their directsuperclasses are at distance 3, and so on. All these arelegitimate superclasses for purposes such as method selection.When superclasses are defined by including the names ofsuperclasses in the \code{contains=} argument to\code{\link{setClass}}, an object from the class will have all theslots defined for its own class \emph{and} all the slots definedfor all its superclasses as well.The information about the relation between a class and aparticular superclass is encoded as an object of class\code{\linkS4class{SClassExtension}}. A list of such objects forthe superclasses (and sometimes for the subclasses) is included inthe metadata object defining the class. If you need to computewith these objects (for example, to compare the distances), callthe function \code{\link{extends}} with argument \code{fullInfo=TRUE}.}\item{Prototype:}{The objects from a class created by a call to\code{\link{new}}are defined by the \emph{prototype} object for the class and byadditional arguments in the call to \code{\link{new}}, which arepassed to a method for that class for the function\code{\link{initialize}}.Each class representation object contains a prototype objectfor the class (although for a virtual class the prototype may be\code{NULL}). The prototype object must have values for all theslots of the class.By default, these are the prototypes of the corresponding slotclasses. However, the definition of the class can specify anyvalid object for any of the slots.}}}\section{Basic classes}{There are a number of \sQuote{basic} classes, corresponding to theordinary kinds of data occurring in \R. For example,\code{"numeric"} is a class corresponding to numeric vectors.The other vector basic classes are \code{"logical"}, \code{"integer"},\code{"complex"}, \code{"character"}, \code{"raw"}, \code{"list"}and \code{"expression"}.The prototypes forthe vector classes are vectors of length 0 of the correspondingtype. Notice that basic classes are unusual in that theprototype object is from the class itself.In addition to the vector classes there are also basic classescorresponding to objects in thelanguage, such as \code{"function"} and \code{"call"}.These classes are subclasses of the virtual class \code{"language"}.Finally, there are object types and corresponding basic classes for\dQuote{abnormal} objects, such as \code{"environment"} and\code{"externalptr"}.These objects do not follow thefunctional behavior of the language; in particular, they are notcopied and so cannot have attributes or slots defined locally.All these classes can be used as slots or assuperclasses for any other class definitions, although they donot themselves come with an explicit class. For the abnormalobject types, a special mechanism is used to enable inheritanceas described below.}% S3 classes\section{Inheriting from non-S4 Classes}{A class definition can extend classes other thanregular S4 classes, usually by specifying them in the\code{contains=} argument to \code{\link{setClass}}. Three groupsof such classes behave distinctly:\enumerate{\itemS3 classes, which must have been registered by a previous call to\code{\link{setOldClass}} (you can check that this has been doneby calling \code{\link{getClass}}, which should return a class thatextends \linkS4class{oldClass});\itemOne of the \R object types, typically a vector type, which thendefines the type of the S4 objects, but also a type such as\code{\link{environment}} that can not be used directly as a typefor an S4 object. Seebelow.\itemOne of the pseudo-classes \code{\linkS4class{matrix}}and \code{\linkS4class{array}}, implying objects witharbitrary vector types plus the \code{dim} and \code{dimnames}attributes.}This section describes the approach to combining S4 computationswith older S3 computations by using such classes as superclasses. Thedesign goal is to allow the S4 class to inherit S3 methods anddefault computations in as consistent a form as possible.As part of a general effort to make the S4 and S3 code in R moreconsistent, when objects from an S4 class are used as the firstargument to a non-default S3 method, either for an S3 generic function(one that calls \code{\link{UseMethod}}) or for one of the primitivefunctions that dispatches S3 methods, an effort is made to provide avalid object for that method. In particular, if the S4 class extendsan S3 class or \code{matrix} or \code{array}, and there is an S3method matching one of these classes, the S4 object will be coerced toa valid S3 object, to the extent that is possible given that there isno formal definition of an S3 class.For example, suppose \code{"myFrame"} is an S4 class that includes theS3 class \code{"data.frame"} in the \code{contains=} argument to\code{\link{setClass}}. If an object from this S4 class is passed toa function, say \code{\link{as.matrix}}, that has an S3 method for\code{"data.frame"}, the internal code for \code{\link{UseMethod}}will convert the object to a data frame; in particular, to an S3object whose class attribute will be the vector corresponding to theS3 class (possibly containing multiple class names). Similarly for anS4 object inheriting from \code{"matrix"} or \code{"array"}, the S4object will be converted to a valid S3 matrix or array.Note that the conversion is \emph{not} applied when an S4 object ispassed to the default S3 method. Some S3 generics attempt to dealwith general objects, including S4 objects. Also, no transformationis applied to S4 objects that do not correspond to a selected S3method; in particular, to objects from a class that does not containeither an S3 class or one of the basic types. See \code{\link{asS4}}for the transformation details.In addition to explicit S3 generic functions, S3 methods aredefined for a variety of operators and functions implemented asprimitives. These methods are dispatched by some internal Ccode that operates partly through the same code as real S3generic functions and partly via special considerations (forexample, both arguments to a binary operator are examined whenlooking for methods). The same mechanism for adapting S4objects to S3 methods has been applied to these computations aswell, with a few exceptions such as generating an error if an S4object that does not extend an appropriate S3 class or type ispassed to a binary operator.The remainder of this section discusses the mechanisms forinheriting from basic object types. See \code{\linkS4class{matrix}}or \code{\linkS4class{array}}for inhering from the matrix and arraypseudo-classes, or from time-series. For thecorresponding details for inheritancefrom S3 classes, see \code{\link{setOldClass}}.An object from a class that directly and simply contains oneof the basic object types in \R, has implicitly a corresponding\code{.Data} slot of that type, allowing computations to extractor replace the data part while leaving other slotsunchanged. If the type is one that can accept attributes and isduplicated normally, the inheritance also determines the type of theobject; if the class definition has a \code{.Data} slotcorresponding to a normal type, the class of theslot determines the type of the object (that is, the value of\code{\link{typeof}(x)}).For such classes, \code{.Data} is a pseudo-slot; thatis, extracting or setting it modifies the non-slot data in theobject. The functions \code{\link{getDataPart}} and\code{\link{setDataPart}} are a cleaner, but essentiallyequivalent way to deal with the data part.Extending a basic type this way allows objects touse old-style code for the corresponding type as well as S4methods. Any basic type can be used for \code{.Data}, buta few types are treated differently because they do not behave like ordinary objects;for example, \code{"NULL"}, environments, and external pointers.Classes extend these types by having a slot, \code{.xData},itself inherited from an internally defined S4 class. Thisslot actually contains an object of the inherited type, toprotect computations from the reference semantics of the type.Coercing to the nonstandard object type then requires anactual computation, rather than the \code{"simple"} inclusionfor other types and classes. The intent is that programmerswill not need to take account of the mechanism, but oneimplication is that you should \emph{not} explicitly use thetype of an S4 object to detect inheritance from an arbitraryobject type. Use\code{\link{is}} and similar functions instead.}\references{Chambers, John M. (2016)\emph{Extending R},Chapman & Hall.(Chapters 9 and 10.)}\seealso{\code{\link{Methods_Details}} for analogous discussion of methods,\code{\link{setClass}} for details of specifying class definitions,\code{\link{is}},\code{\link{as}},\code{\link{new}},\code{\link{slot}}}\keyword{programming}\keyword{classes}\keyword{methods}