Rev 41508 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{slot}\alias{slot}\alias{slot<-}\alias{@<-}\alias{slotNames}\alias{.slotNames}\title{The Slots in an Object from a Formal Class}\description{These functions return or set information about the individual slotsin an object.}\usage{object@nameobject@name <- valueslot(object, name)slot(object, name, check = TRUE) <- valueslotNames(x)}%% .slotNames(x)%%%% NOTA BENE: We shouldn't advertize .slotNames() --%% - - - - - rather slotNames() should be changed {and getSlots() too}%% such that it doesn't special-case class representations!%%\arguments{\item{object}{An object from a formally defined class.}\item{name}{The character-string name of the slot. The name must be avalid slot name: see Details below.}\item{value}{A new value for the named slot. The value must bevalid for this slot in this object's class.}\item{x}{Either the name of a class or an object from that class.Print \code{\link{getClass}(class)} to see the full description ofthe slots.}\item{check}{If \code{TRUE}, check the assigned value for validityas the value of this slot. You should never set this to\code{FALSE} in normal use, since the result can create invalid objects.}}\details{The \code{"@"} operator and the \code{slot} function extract orreplace the formally defined slots for the object. The operatortakes a fixed name, which can be unquoted if it is syntactically aname in the language. A slot name can be any non-empty string, butif the name is not made up of letters, numbers, and \code{.}, itneeds to be quoted (by backticks or single or double quotes).In the case of the \code{slot} function, \code{name} can be anyexpression that evaluates to a valid slot in the class definition.Generally, the only reason to use the functional form rather thanthe simpler operator is \emph{because} the slot name has to be computed.The definition of the class contains the names of all slots directlyand indirectly defined. Each slot has a name and an associatedclass. Extracting a slot returns an object from that class. Settinga slot first coerces the value to the specified slot and then storesit.Unlike attributes, slots are not partially matched, and asking for (ortrying to set) a slot with an invalid name for that class generates anerror.Note that currently, \code{slotNames()} behaves particularly for classrepresentation objects -- this is considered bogus and likely to bechanged.Currently the \code{\link{@}} extraction operator and \code{slot}function do no checking, neither that \code{object} has a formal classnor that \code{name} is a valid slot name for the class. (They willextract the attribute of the given name if it exists from any \R object.)The replacement forms do check (at least if \code{check=TRUE}).}\references{The R package \pkg{methods} implements, with a few exceptions, theprogramming interface for classes and methods in the book\emph{Programming with Data} (John M. Chambers, Springer, 1998), inparticular sections 1.6, 2.7, 2.8, and chapters 7 and 8.While the programming interface for the \pkg{methods} package followsthe reference, the R software is an original implementation, sodetails in the 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 see \code{?\link{Methods}}and the links from that documentation.}\seealso{\code{\link{@}},\code{\link{Classes}},\code{\link{Methods}},\code{\link{getClass}}}\examples{\dontshow{if(isClass("track")) removeClass("track")}setClass("track", representation(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)slotNames("track") # is the same asslotNames(myTrack)%% Don't do this --- this is a bug% ## sophisticated:% clDef <- getClass(class(myTrack))% slotNames(clDef) # "x" and "y" -- as for myTrack% .slotNames(clDef)# the slots of the class definition object itself\dontshow{removeClass("track")## should not be needed... see ./setClass.Rd}}\keyword{programming}\keyword{classes}