Rev 46659 | 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}\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}).One class name is special, \code{.Data}. This stands for the\sQuote{data part} of the object. Any class that 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. The \code{.Data} slot also determines the type of theobject; if \code{x} has a \code{.Data} slot, the type of theslot is the type of the object (that is, the value of\code{\link{typeof}(x)}). 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}, with theexception of a few that do not behave like ordinary objects;namely, \code{"NULL"}, environments, and external pointers.There is one additional use of the data part, which is also anexception to the correspondence with the object's type. The exceptionarises from the special treatment of \code{\linkS4class{matrix}}and \code{\linkS4class{array}} \dQuote{classes} in \R.Matrix and array objects are managed internally and recognizedwithout regard to any class attribute; therefore, they can beused as the data part of a new class. In this case, the objecttype for the new class depends on the type of the data in thematrix or array.If the new class does not have a data part as described above,the type of objects from the new class is\code{"S4"}.}\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, typically created by a call to\code{\link{new}} or by assigning another object from the class,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 definition contains a prototype objectfor the class. This must have values for all the slots defined bythe class definition.By default, these are the prototypes of allthe slot classes, if those are not virtual classes. However, thedefinition of the class can specify any valid object for any ofthe slots.}}}\section{Virtual classes; Basic classes}{Classes exist for which there are no actual objects, 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 data 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 basic classes forspecialized objects, such as \code{"environment"} and \code{"externalptr"}.The vector and language basic classes can be used as slots or assuperclasses for any other class definitions.The classes corresponding to other object types can be used asslots but not always as superclasses, since many of them do not follow thefunctional behavior of the language; in particular, they are notcopied and so cannot have attributes or slots defined locally.}\section{S3 Classes}{Earlier, informal classes of objects (usually referred to as\sQuote{S3} classes) are used by many \R functions. It is natural toconsider including them as the class for a slot in a formal class,or even as a class to be extended by the new class. This isn'tprohibited but there are some disadvantages, and if you do want toinclude S3 classes, they should be declared by including them in acall to \code{\link{setOldClass}}. Here are some considerations:\itemize{\item Using S3 classes somewhat defeats the purpose of defining aformal class: An important advantage to your users is that a formalclass provides guarantees of what the object contains (minimally,the classes of the slots and therfore what data they contain;optionally, any other requirements imposed by a validity method).But there is no guarantee whatever about the data in an object froman S3 class. It's entirely up to the functions that create ormodify such objects. If you want to provide guarantees to yourusers, you will need a valdity method that explicitly checks thecontents of S3-class objects.\item To get the minimal guarantee (that the object in a slot has, orextends, the class for the slot) you should ensure that the S3classes are known to \emph{be} S3 classes, possibly with Sinheritance. To do this, include a call to\code{\link{setOldClass}} for the S3 classes used.Otherwise, the S3 class is undefined (and the code used by\code{setClass} will issue a warning). Slot assignments, forexample, will not then check for possible errors.\item Current versions of \R (beginning with 2.8.0) try toaccommodate S4 classes that extend S3 classes, that is, thoseincluding an S3 class in the \code{contains=} argument to\code{\link{setClass}} . Specifically, objects from such classeswill contain the S3 class as a slot, and some S3 computations willrecognize the S3 class,including method dispatch and the function\code{\link{inherits}}. See \code{\link{S3Class}} for details.The S3 classes \emph{must} have been registered by a call to \code{\link{setOldClass}}.The basic caution remains true however: There is no guarantee that all S3 computations will becompatible, and some are known not to be.\item These caveats apply to S3 classes; that is, objects with aclass assigned by some \R function but without a formal classdefinition. In contrast, the built-in vector types (\code{numeric},\code{list}, etc.) are generally fine as slots or for\code{contains=} classes. These objecttypes don't have formal slots, but the base code in the systemessentially forces them to contain the type of data they claim tohave.Objects with a \dQuote{class} of \code{matrix} or \code{array} are somewhat inbetween. They do not have an explicit S3 class, but do have one ortwo attributes. There is no general problem in having these asslots, but because there is no guarantee of a dimnames slot, theydon't work as formal classes. The \code{ts} class, although alsoancient in the S language, is implemented in \R essentially as anS3 class, with the implications noted above---not suitable for a\code{contains=} argument---but with a few S4methods defined. See the documentation for class \code{\linkS4class{structure}} for more details.}}% S3 classes\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}