Rev 15561 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{assign}\alias{assign}\alias{<-}\alias{=}\alias{<<-}\alias{->}\alias{->>}\title{Assign a Value to a Name}\description{Assign a value to a name in an environment.}\usage{assign(x, value, pos = -1, envir = as.environment(pos),inherits = FALSE, immediate = TRUE)x <- valuex <<- valuevalue -> xvalue ->> xx = value}\arguments{\item{x}{a variable name (given as a quoted string in the function call).}\item{value}{a value to be assigned to \code{x}.}\item{pos}{where to do the assignment. By default, assigns into thecurrent environment. See the details for other possibilities.}\item{envir}{the \code{\link{environment}} to use. See the details section.}\item{inherits}{should the enclosing frames of the environment beinspected?}\item{immediate}{an ignored compatibility feature.}}\details{The \code{pos} argument can specify the environment in which to assignthe object in any of several ways:as an integer (the position in the \code{\link{search}} list); asthe character string name of an element in the search list; or as an\code{\link{environment}} (including using \code{\link{sys.frame}} toaccess the currently active function calls).The \code{envir} argument is an alternative way to specify anenvironment, but is primarily there for back compatibility.\code{assign} does not dispatch assignment methods, so it cannot beused to set elements of vectors, names, attributes, etc.}\section{Assignment Operators}{There are three different assignmentoperators. The operators \code{<-} and \code{=} assign into theenvironment in which they are evaluated. The \code{<-} can be usedanywhere, but the \code{=} is only allowed at the top level (thatis, in the complete expression typed by the user) or as one of thesubexpressions in a braced list of expressions.The operators \code{<<-} and \code{->>} cause a search to madethrough the environment for an existingdefinition of the variable being assigned. If such a variable isfound then its value is redefined, otherwise assignment takes placeglobally. Note that their differs from that inthe S language, but is useful in conjunction with the scoping rules ofR.In all the assignment operator expressions, \code{x} can be a nameor an expression defining a part of an object to be replaced (e.g.,\code{z[[1]]}). The name does not need to be quoted, though it canbe.}\value{This function is invoked for its side effect, which is assigning\code{value} to the variable \code{x}. If no \code{envir} isspecified, then the assignment takes place in the currently activeenvironment.If \code{inherits} is \code{TRUE}, enclosing environments of the suppliedenvironment are searched until the variable \code{x} is encountered.The value is then assigned in the environment in which the variable isencountered. If the symbol is not encountered then assignment takesplace in the user's workspace (the global environment).If \code{inherits} is \code{FALSE}, assignment takes place in theinitial frame of \code{envir}.}\seealso{\code{\link{get}},\code{\link{exists}},\code{\link{environment}}.}\examples{for(i in 1:6) { #-- Create objects 'r1', 'r2', ... 'r6' --nam <- paste("r",i, sep=".")assign(nam, 1:i)}ls(pat="^r..$")##-- Global assignment within a function:myf <- function(x) {innerf <- function(x) assign("Global.res", x^2, env = .GlobalEnv)innerf(x+1)}myf(3)Global.res # 16a<-1:4assign("a[1]",2)a[1]==2 #FALSEget("a[1]")==2 #TRUE}\keyword{data}