is {methods} | R Documentation |
Is an Object from a Class
Description
is
:
With two arguments, tests whether object
can be treated as from
class2
.
With one argument, returns all the super-classes of this object's class.
extends
:
Does the first class extend the second class?
Returns maybe
if the extension includes a test.
setIs
:
Defines class1
to be an extension of class2
.
Usage
is(object, class2)
extends(class1, class2, maybe=TRUE, fullInfo = FALSE)
setIs(class1, class2, test=NULL, coerce=NULL, replace=NULL,
by = character(), where = topenv(parent.frame()), classDef =,
extensionObject = NULL, doComplete = TRUE)
Arguments
object |
any R object. |
class1 , class2 |
the names of the classes between which |
maybe , fullInfo |
In a call to |
extensionObject |
alternative to the |
doComplete |
when |
test , coerce , replace |
In a call to |
by |
In a call to |
where |
In a call to |
classDef |
Optional class definition for |
Details
extends
:-
Given two class names,
extends
by default says whether the first class extends the second; that is, it does for class names whatis
does for an object and a class. Given one class name, it returns all the classes that class extends (the “superclasses” of that class), including the class itself. If the flagfullInfo
isTRUE
, the result is a list, each element of which is an object describing the relationship; otherwise, and by default, the value returned is only the names of the classes. setIs
:-
This function establishes an inheritance relation between two classes, by some means other than having one class contain the other. It should not be used for ordinary relationships: either include the second class in the
contains=
argument tosetClass
if the class is contained in the usual way, or considersetClassUnion
to define a virtual class that is extended by several ordinary classes. A call tosetIs
makes sense, for example, if one class ought to be automatically convertible into a second class, but they have different representations, so that the conversion must be done by an explicit computation, not just be inheriting slots, for example. In this case, you will typically need to provide both acoerce=
andreplace=
argument tosetIs
.The
coerce
,replace
, andby
arguments behave as described for thesetAs
function. It's unlikely you would use theby
argument directly, but it is used in defining cached information about classes. The value returned (invisibly) bysetIs
is the extension information, as a list.The
coerce
argument is a function that turns aclass1
object into aclass2
object. Thereplace
argument is a function of two arguments that modifies aclass1
object (the first argument) to replace the part of it that corresponds toclass2
(supplied asvalue
, the second argument). It then returns the modified object as the value of the call. In other words, it acts as a replacement method to implement the expressionas(object, class2) <- value
.The easiest way to think of the
coerce
andreplace
functions is by thinking of the case thatclass1
containsclass2
in the usual sense, by including the slots of the second class. (To repeat, in this situation you would not callsetIs
, but the analogy shows what happens when you do.)The
coerce
function in this case would just make aclass2
object by extracting the corresponding slots from theclass1
object. Thereplace
function would replace in theclass1
object the slots corresponding toclass2
, and return the modified object as its value.The relationship can also be conditional, if a function is supplied as the
test
argument. This should be a function of one argument that returnsTRUE
orFALSE
according to whether the object supplied satisfies the relationis(object, class2)
. If you worry about such things, conditional relations between classes are slightly deprecated because they cannot be implemented as efficiently as ordinary relations and because they sometimes can lead to confusion (in thinking about what methods are dispatched for a particular function, for example). But they can correspond to useful distinctions, such as when two classes have the same representation, but only one of them obeys certain additional constraints.Because only global environment information is saved, it rarely makes sense to give a value other than the default for argument
where
. One exception iswhere=0
, which modifies the cached (i.e., session-scope) information about the class. Class completion computations use this version, but don't use it yourself unless you are quite sure you know what you're doing.
References
The R package methods implements, with a few exceptions, the programming interface for classes and methods in the book Programming with Data (John M. 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 in the reference that reflect the S4 implementation may appear differently in R. Also, there are extensions to the programming interface developed more recently than the reference. For a discussion of details and ongoing development, see the web page http://developer.r-project.org/methodsPackage.html and the pointers from that page.
Examples
## 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" slots
setClass("trackMultiCurve",
representation(x="numeric", y="matrix", smooth="matrix"),
prototype = structure(list(), x=numeric(), y=matrix(0,0,0),
smooth= matrix(0,0,0)))
## Automatically convert an object from class "trackCurve" into
## "trackMultiCurve", by making the y, smooth slots into 1-column matrices
setIs("trackCurve",
"trackMultiCurve",
coerce = function(obj) {
new("trackMultiCurve",
x = obj@x,
y = as.matrix(obj@y),
curve = as.matrix(obj@smooth))
},
replace = function(obj, value) {
obj@y <- as.matrix(value@y)
obj@x <- value@x
obj@smooth <- as.matrix(value@smooth)
obj})
## Automatically convert the other way, but ONLY
## if the y data is one variable.
setIs("trackMultiCurve",
"trackCurve",
test = function(obj) {ncol(obj@y) == 1},
coerce = function(obj) {
new("trackCurve",
x = slot(obj, "x"),
y = as.numeric(obj@y),
smooth = as.numeric(obj@smooth))
},
replace = function(obj, value) {
obj@y <- matrix(value@y, ncol=1)
obj@x <- value@x
obj@smooth <- value@smooth
obj})