slot
Identify, Extract, or Replace Data in Objects With Slots

Description

Extracts the named slot from an object or replaces the contents of the slot with the specified replacement value.

Usage

slot(object, name)
slot(object, name, check = TRUE) <- value
slotNames(x)
.slotNames(x)
getSlots(x)
.hasSlot(object, name)

Arguments

object an object containing slots.
name an expression evaluating to the name of a slot for the class of object.
check a logical value. If TRUE (the default), checks that value is valid for slot name. If this argument is TRUE, and the class of value is not the class or a subclass of the slot, an error is generated.
x either the name of the class as a string, or an S4 object.
value the new value to be replaced.
Value
slotreturns the contents of the specified slot for slot().
slotNamesreturns the character vector of the names of the slots for an S4 class. If x is a string giving the name of an S4 class, it returns the slot names for that class. If x is an S4 object, it returns the slot names for that object's class. If x is a classRepresentation object, it returns the slot names from the specified class, rather than the slots in the classRepresentation class.
.slotNamesreturns the character vector of the names of the slots for an S4 class. This is the same as slotNames, except that if x is a classRepresentation object, slotNames returns the slot names from the classRepresentation class itself.
getSlotsreturns a named character vector, whose elements are the classes of the slots, and whose names are the names of the slots. x must be either a string giving the name of an S4 class, or a classRepresentation object.
.hasSlotreturns TRUE if name is one of the attribute names of object. This detects slots defined in the object class, as well as other attributes added to the object.
See Also
Subscript, @.
Examples
setClass("myclass", representation(x = "numeric", y = "numeric"))
myobj <- new("myclass", x=1, y=2)
slot(myobj, "y")      # returns 2
slot(myobj, "y") <- 3
slot(myobj, "y")      # returns 3

slot(myobj, "x") <- "ABC" # Error since "ABC" is not numeric slot(myobj, "x", check = FALSE) <- "ABC" # OK. slot(myobj, "x") # "ABC"

slot(myobj, "z") <- "abc" # Error: "z" is not a slot in class "myclass" slot(myobj, "z", check = FALSE) <- "abc" # OK. slot(myobj, "z") # "abc"

slotNames("myclass") # [1] "x" "y" getSlots("myclass") # x y # "numeric" "numeric" .hasSlot(myobj, "x") # TRUE .hasSlot(myobj, "z") # TRUE (since it has been added as an attribute) .hasSlot(myobj, "nosuchslot") # FALSE

Package methods version 6.1.1-7
Package Index