Rev 21345 | Rev 24049 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{is}\alias{is}\alias{extends}\alias{setIs}\title{Is an Object from a Class}\description{\code{is}:With two arguments, tests whether \code{object} can be treated as from\code{class2}.With one argument, returns all the super-classes of this object's class.\code{extends}:Does the first class extend the second class?Returns \code{maybe} if the extension includes a test.\code{setIs}:Defines \code{class1} to be an extension of \code{class2}.}\usage{is(object, class2)extends(class1, class2, maybe=TRUE)setIs(class1, class2, test=NULL, coerce=NULL, replace=NULL,by = NULL, where = 1, complete = TRUE)}\arguments{\item{object}{Any R object.}\item{class1, class2}{The names of the classes between which \code{is} relations are to bedefined.}\item{maybe}{What value to return if the relationship is conditional.}\item{test, coerce, replace}{Functions optionally supplied to test whether the relation isdefined, to coerce the object to \code{class2}, and to alter theobject so that \code{is(object, class2)} is identical to\code{value}.}\item{by}{The name of an intermediary class. Coercion will proceed by firstcoercing to this class and from there to the target class. (Theintermediate coercions have to be valid.)}\item{where}{Where to store the metadata defining the relationship. Default isthe global environment.}\item{complete}{Should the class definitions be completed when checking for validrelations. \emph{Don't} set this to anything other than\code{TRUE}, unless you really know why you're doing it (used by\code{setClass}).}}\details{\code{setIs}:The relationship can be conditional, if a function is supplied as the \code{test}argument. If a function is supplied as the \code{coerce} argument, this function willbe applied to any \code{class1} object in order to turn it into a\code{class2} object.If the relationship is to be defined indirectly through a third class,this class can be named in the \code{by} argument.Extension may imply that a \code{class1} object contains a \code{class2} object. The defaultsense of containing is that all the slots of the simpler class are found in themore elaborate one. If the \code{replace} argument is supplied as an S replacementfunction, this function will be used to implement \code{as(obj, class2)<- value}.The \code{coerce}, \code{replace}, and \code{by} arguments behaveas described for the \code{\link{setAs}} function. It's unlikely youwould use the \code{by} argument directly, but it is used in definingcached information about classes. The value returned (invisibly) by\code{setIs} is the extension information, as a list.Information about \code{setIs} relations can be stored in the metadatafor either \code{class1} (in the \code{extends} information) or in themetadata for \code{class2} (in the \code{subclasses} information). Forthe information to be retained for a future session, one of theseclasses must be defined in the globalenvironment, since only objects assigned there are saved by\code{\link{save.image}}. If neither class is defined in environment\code{where}, \code{setIs} generates an error.Because only global environment information is saved, it rarely makessense to give a value other than the default for argument \code{where}.One exception is \code{where=0}, which modifies the cached (i.e.,session-scope) information about the class. Class completioncomputations use this version, but don't use it yourself unless you arequite sure you know what you're doing.}\references{The R package \code{methods} implements, with a few exceptions, theprogramming interface for classesand methods in the book \emph{Programming with Data} (JohnM. Chambers, Springer, 1998), in particular sections 1.6, 2.7, 2.8,and chapters 7 and 8.While the programming interface for the methods package follows the reference,the R software is an original implementation, so details inthe reference that reflect the S4 implementation may appeardifferently in R. Also, there are extensions to the programminginterface developed more recently than the reference. For adiscussion of details and ongoing development, see the web page\url{http://developer.r-project.org/methodsPackage.html} and thepointers from that page.}\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 slot}## a class definition (see \link{setClass} for the example)setClass("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 = structure(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")))})}\keyword{programming}\keyword{classes}\keyword{methods}