These functions return or set information about the individual slots in an object.
1 2 3 4 5 6 7 8 9
An object from a formally defined class.
The name of the slot. The operator
takes a fixed name, which can be unquoted if it is syntactically a
name in the language. A slot name can be any non-empty string, but
if the name is not made up of letters, numbers, and
In the case of the
A new value for the named slot. The value must be valid for this slot in this object's class.
In the replacement version of
either the name of a class (as character string), or a class
definition. If given an argument that is neither a character string
nor a class definition,
The definition of the class specifies all slots directly and indirectly defined for that class. Each slot has a name and an associated class. Extracting a slot returns an object from that class. Setting a slot first coerces the value to the specified slot and then stores it.
Unlike general attributes, slots are not partially matched, and asking for (or trying to set) a slot with an invalid name for that class generates an error.
@ extraction operator and
function themselves do no checking against the class definition,
simply matching the name in the object itself.
The replacement forms do check (except for
slot in the case
check=FALSE). So long as slots are set without cheating, the
extracted slots will be valid.
Be aware that there are two ways to cheat, both to be avoided but
with no guarantees. The obvious way is to assign a slot with
check=FALSE. Also, slots in R are implemented as
attributes, for the sake of some back compatibility. The current
implementation does not prevent attributes being assigned, via
attr<-, and such assignments are not checked for
legitimate slot names.
Note that the
"@" operators for extraction and replacement are
primitive and actually reside in the base package.
The replacement versions of
slot() differ in
the computations done to coerce the right side of the assignment to
the declared class of the slot. Both verify that the value provided
is from a subclass of the declared slot class. The
version will go on to call the coerce method if there is one, in
effect doing the computation
as(value, slotClass, strict =
"@" version just verifies the relation,
leaving any coerce to be done later (e.g., when a relevant method is
In most uses the result is equivalent, and the
saves an extra function call, but if empirical evidence shows that a
conversion is needed, either call
as() before the replacement
or use the replacement version of
"@" operator and the
slot function extract or
replace the formally defined slots for the object.
getSlots return respectively the
names of the slots and the classes associated with the slots in the
specified class definition. Except for its extended interpretation of
slotNames(x) is just
Chambers, John M. (2008) Software for Data Analysis: Programming with R Springer. (For the R version.)
Chambers, John M. (1998) Programming with Data Springer (For the original S4 version.)
1 2 3 4 5 6 7 8 9 10
setClass("track", slots = c(x="numeric", y="numeric")) myTrack <- new("track", x = -4:4, y = exp(-4:4)) slot(myTrack, "x") slot(myTrack, "y") <- log(slot(myTrack, "y")) utils::str(myTrack) getSlots("track") # or getSlots(getClass("track")) slotNames(class(myTrack)) # is the same as slotNames(myTrack)