Rev 16074 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{setClass}\alias{setClass}\alias{removeClass}\alias{isClass}\alias{getClasses}\alias{unClass}\title{Create a Class Definition}\description{Create a formally defined class with specified slots and/orrelationships to other classes. Also functions to test whether aclass has been defined, and to remove a class definition}\usage{setClass(Class, representation, prototype,contains=character(), validity, access, where=1, version=FALSE)isClass(Class, formal=TRUE)removeClass(Class, where=-1)getClasses(where)unclass(x)}\arguments{\item{Class}{ character string name for the class }\item{representation}{ the slots that the new class should haveand/or other classes that this class extends. Usually a call tothe \code{\link{representation}} function. }\item{prototype}{ an object (usually a list) providing the defaultdata for the slots specified in the representation. }\item{contains}{ what classes does this class extend? (These arecalled \emph{superclasses} in some languages.) When theseclasses have slots, all their slots will be contained in the newclass as well. }\item{where}{ What environment to use to store or remove the definition (as metadata).By default, uses the global environment for \code{setClass} andsearches for a definition to remove, for \code{removeClass}. }\item{validity, access, version}{ Control arguments included forcompatibility with the S-Plus API, but not currently used. }\item{x}{an arbitrary object.}}\details{These are the functions that create and manipulate formal classdefinitions. Brief documentation is provided below. See thereferences for an introduction and for more details.\describe{\item{\code{setClass}:}{Define Class to be an S-style class. The effect is to create anobject, of class \code{"classRepEnvironment"}, and store this(hidden) in the specified environment or database. Objects can becreated from the class (e.g., by calling \code{\link{new}}),manipulated (e.g., by accessing the object's slots), and methodsmay be defined including the class name in the signature (see\code{\link{setMethod}}).}\item{\code{removeClass}:}{Remove the definition of this class. Calling this \emph{always}resets the version of the class cached for the session. If\code{where=0}, that's all it does. Otherwise, it removes theversion from the specified environment or database (from the globalenvironment by default).}\item{\code{isClass}:}{Is this a the name of a formally defined class? (Argument \code{formal} is forcompatibility and is ignored.)}\item{\code{getClasses}:}{The names of all the classes formally defined on `where'.If called with no argument, all the classes currently known in the session(which does not include classes that may be defined on one of the attachedlibraries, but have not yet been used in the session).}\item{\code{unclass}:}{Returns the object containing the values of all the slots in this object'sclass definition (specifically, ithe returned object has attributes correspondingto each slot), in the case that the object's class is formally defined with slots.For classes that extend a single other class (e.g., a basic classsuch as \code{"numeric"}) the result is a object of that class.}}}\section{Inheritance and Prototypes}{Defining new classes that inherit from (``extend'') other classes is apowerful technique, but has to be used carefully and not over-used.Otherwise, you will often get unintended results when you start tocompute with objects from the new class.As shown in the examples below, the simplest and safest form ofinheritance is to start with an explicit class, with some slots, thatdoes not extend anything else. It only does what we say it does.Then extensions will add some new slots and new behavior.Another variety of extension starts with one of the basic classes,perhaps with the intension of modifying R's standard behavior for thatclass. Perfectly legal and sometimes quite helpful, but you may needto be more careful in this case: your new class will inherit much ofthe behavior of the basic (informally defined) class, and the resultscan be surprising. Just proceed with caution and plenty of testing.As an example, the class \code{"matrix"} is included in thepre-defined classes, to behave essentially as matrices do withoutformal class definitions. Suppose we don't like all of this; inparticular, we want the default matrix to have 0 rows and columns (not1 by 1 as it is now).\code{setClass("myMatrix", "matrix", prototype = matrix(0,0,0))}The arguments above illustrate two short-cuts relevant to suchexamples. We abbreviated the \code{representation} argument to thesingle superclass, because the new class doesn't add anything to therepresentation of class \code{"matrix"}. Also, we provided an objectfrom the superclass as the prototype, not a list of slots.}\references{The web page \url{http://www.omegahat.org/RSMethods/index.html}is the primary documentation.The functions in this package emulate the facility for classesand methods described in\emph{Programming with Data}, (John M. Chambers, Springer, 1998).See this book for further details and examples.}\author{John Chambers}\examples{\testonly{if(isClass("track"))removeClass("track")if(isClass("trackCurve"))removeClass("trackCurve")if(isClass("trackMultiCurve"))removeClass("trackMultiCurve")}## A simple class with two slotssetClass("track",representation(x="numeric", y="numeric"))## A class extending the previous, adding one more slotsetClass("trackCurve",representation("track", smooth = "numeric"))## A class similar to "trackCurve", but with different structure## allowing matrices for the "y" and "smooth" slotssetClass("trackMultiCurve", representation(x="numeric", y="matrix", smooth="matrix"),prototype = list(x=numeric(), y=matrix(0,0,0), smooth= matrix(0,0,0)))## Define a multi-curve to extend a single curve ONLY## if the y data is one variable.setIs("trackMultiCurve", "trackCurve", test = function(obj) {ncol(slot(obj, "y")) == 1},coerce = function(obj) { new("trackCurve", x = slot(obj, "x"),y = as.numeric(slot(obj,"y")), curve = as.numeric(slot(obj, "curve")))})\testonly{tMC <- new("trackMultiCurve")is.matrix(slot(tMC, "y"))is.matrix(slot(tMC, "smooth"))setClass("myMatrix", "matrix", prototype = matrix(0,0,0))nrow(new("myMatrix")) # 0nrow(new("matrix")) # 1}}\seealso{\code{\link{Methods}}, \code{\link{setSClass}}}\keyword{programming}\keyword{classes}\keyword{methods}