Rev 51966 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/Classes.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{Classes}\alias{Classes}\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.}\item{Superclasses:}{The definition of a class includes the \emph{superclasses} ---the classes 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 isexpressed 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 the slotsdefined for its own class \emph{and} all the slots defined for allits 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 ofthe corresponding slot classes. However, thedefinition of the class can specify any valid object for any ofthe slots.}}}\section{Virtual classes; Basic classes}{Classes exist for which no actual objects can be created by acall to \code{\link{new}}, the\emph{virtual} classes, in fact avery important programming tool. They are used to group togetherordinary classes that want to share some programming behavior,without necessarily restricting how the behavior is implemented.Virtual class definitions may if you want includeslots (to provide some common behavior without fully definingthe object---see the class \code{\linkS4class{traceable}} for an example).A simple and useful form of virtual class is the \emph{classunion}, a virtual class that is defined in a call to\code{\link{setClassUnion}} by listing one ormore of subclasses (classes that extend the class union). Classunions can include as subclasses basic object types (whosedefinition is otherwise sealed).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 classes corresponding 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 first argumentto a non-default S3 method, either for an S3 generic function (one that calls\code{\link{UseMethod}}) or for one of the primitive functionsthat dispatches S3 methods, an effort is made to provide avalid object for that method. Inparticular, if the S4 class extends an S3 class or \code{matrix}or \code{array}, and there is an S3 method matching one of theseclasses, the S4 object will be coerced to a valid S3 object, tothe extent that is possible given that there is no formaldefinition of an S3 class.For example, suppose \code{"myFrame"} is an S4 class thatincludes the S3 class \code{"data.frame"} in the \code{contains=}argument to \code{\link{setClass}}. If an object from this S4class is passed to a function, say \code{\link{as.matrix}}, thathas 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 S3 object whose class attribute will be the vectorcorresponding to the S3 class (possibly containing multipleclass names). Similarly for an S4 object inheriting from\code{"matrix"} or \code{"array"}, the S4 object will beconverted to a valid S3 matrix or array.Note that the conversion is \emph{not} applied when an S4 object is passedto the default S3 method. Some S3 generics attemptto deal with general objects, including S4 objects. Also, no transformation isapplied to S4 objects that do not correspond to aselected S3 method; in particular, to objects from a class thatdoes not contain either 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. (2008)\emph{Software for Data Analysis: Programming with R}Springer. (For the R version.)Chambers, John M. (1998)\emph{Programming with Data}Springer (For the original S4 version.)Chambers, John M. and Hastie, Trevor J. eds (1992)\emph{Statistical Models in S.}Wadsworth & Brooks/Cole (Appendix A for S3 classes.)Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth & Brooks/Cole. (Out of print.) (The description ofvectors, matrix, array and time-series objects.)}\seealso{\code{\link{Methods}} 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}