Rev 46128 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/methods/man/slot.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{slot}\alias{slot}\alias{slot<-}\alias{@<-}\alias{slotNames}\alias{.slotNames}\alias{getSlots}\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)getSlots(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 name of the slot. 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.}\item{value}{A new value for the named slot. The value must bevalid for this slot in this object's class.}\item{check}{In the replacement version of \code{slot}, a flag. If \code{TRUE}, check the assigned value for validityas the value of this slot. User's coded should not set this to\code{FALSE} in normal use, since the resulting object can be invalid.}\item{x}{Either the name of a class or a class definition.If given an argument that is neither a character string nor aclass definition, \code{slotNames} (only) uses \code{class(x)} instead.}}\value{The \code{"@"} operator and the \code{slot} function extract orreplace the formally defined slots for the object.Functions \code{slotNames} and \code{getSlots} return respectively thenames of the slots and the classes associated with the slots in thespecified class definition. Except for its extended interpretation of\code{x} (above), \code{slotNames(x)} is just \code{names(getSlots(x))}.}\details{The definition of the class specifies all slots directlyand indirectly defined for that class. 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 general attributes, slots are not partially matched, and asking for (ortrying to set) a slot with an invalid name for that class generates anerror.The \code{\link{@}} extraction operator and \code{slot}function themselves do no checking against the class definition,simply matching the name in the object itself.The replacement forms do check (except for \code{slot} in the case\code{check=FALSE}). So long as slots are set without cheating, theextracted slots will be valid.Be aware that there are two ways to cheat, both to be avoided butwith no guarantees. The obvious way is to assign a slot with\code{check=FALSE}. Also, slots in \R are implemented asattributes, for the sake of some back compatibility. The currentimplementation does not prevent attributes being assigned, via\code{\link{attr<-}}, and such assignments are not checked forlegitimate slot names.}\references{Chambers, John M. (2008)\emph{Software for Data Analysis: Programming with R}Springer. (For the R version.)Chambers, John M. (1998)\emph{Programming with Data}Springer (For the original S4 version.)}\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)getSlots("track") # orgetSlots(getClass("track"))slotNames(class(myTrack)) # is the same asslotNames(myTrack)\dontshow{removeClass("track")## should not be needed... see ./setClass.Rd}}\keyword{programming}\keyword{classes}