Rev 3988 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{assign}\title{Assign a Value to a Name}\usage{assign(x, value, pos = -1, envir = sys.frame(sys.parent()),inherits = FALSE, immediate = TRUE)x <- valuex <<- valuevalue -> xvalue ->> x}\alias{assign}\alias{<-}\alias{<<-}\alias{->}\alias{->>}\arguments{\item{x}{a variable name (given as a quoted string).}\item{value}{a value to be assigned to \code{x}.}\item{pos}{an index into the search list which determines whichenvironment the assignment is to take place in. A character string mayalso be used. The environmentcan also be specified directly with \code{envir}.}\item{envir}{the \code{\link{environment}} in which to assign. Thedefault is the environment where the call to \code{assign} takes place.}\item{inherits}{should the enclosing frames of the environment beinspected?}\item{immediate}{an ignored compatibility feature.}}\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}, parents 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 global environment.If \code{inherits} is \code{FALSE}, assignment takes place in theinitial frame of \code{envir}.The arrow forms of assignment provide shortcut ways to carry outassignment. The \code{<-} and \code{->} forms carry out assignmentin the local environment frame, while the \code{<<-} and \code{->>}forms cause a search to made through 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 the action of \code{<<-} and \code{->>} differs fromthat in the S language, but is useful in conjunction with thescoping rules of R.}\seealso{\code{\link{get}}, \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 # 16}\keyword{data}