The R Project SVN R

Rev

Rev 61509 | Rev 68948 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 61509 Rev 61511
Line 24... Line 24...
24
  \item{inherits}{should the enclosing frames of the environment be
24
  \item{inherits}{should the enclosing frames of the environment be
25
    inspected?}
25
    inspected?}
26
  \item{immediate}{an ignored compatibility feature.}
26
  \item{immediate}{an ignored compatibility feature.}
27
}
27
}
28
\details{
28
\details{
29
  There are no restrictions on \code{name}: it can be a non-syntactic
29
  There are no restrictions on the name given as \code{x}: it can be a
30
  name (see \code{\link{make.names}}).
30
  non-syntactic name (see \code{\link{make.names}}).
31
 
31
 
32
  The \code{pos} argument can specify the  environment in which to assign
32
  The \code{pos} argument can specify the  environment in which to assign
33
  the object in any of several ways: as \code{-1} (the default),
33
  the object in any of several ways: as \code{-1} (the default),
34
  as a positive integer (the position in the \code{\link{search}} list); as
34
  as a positive integer (the position in the \code{\link{search}} list); as
35
  the character string name of an element in the search list; or as an
35
  the character string name of an element in the search list; or as an
36
  \code{\link{environment}} (including using \code{\link{sys.frame}} to
36
  \code{\link{environment}} (including using \code{\link{sys.frame}} to
37
  access the currently active function calls).
37
  access the currently active function calls).
38
  The \code{envir} argument is an alternative way to specify an
38
  The \code{envir} argument is an alternative way to specify an
39
  environment, but is primarily there for back compatibility.
39
  environment, but is primarily for back compatibility.
40
 
40
 
41
  \code{assign} does not dispatch assignment methods, so it cannot be
41
  \code{assign} does not dispatch assignment methods, so it cannot be
42
  used to set elements of vectors, names, attributes, etc.
42
  used to set elements of vectors, names, attributes, etc.
43
 
43
 
44
  Note that assignment to an attached list or data frame changes the
44
  Note that assignment to an attached list or data frame changes the
Line 59... Line 59...
59
  symbol is not encountered then assignment takes place in the user's
59
  symbol is not encountered then assignment takes place in the user's
60
  workspace (the global environment).
60
  workspace (the global environment).
61
 
61
 
62
  If \code{inherits} is \code{FALSE}, assignment takes place in the
62
  If \code{inherits} is \code{FALSE}, assignment takes place in the
63
  initial frame of \code{envir}, unless an existing binding is locked or
63
  initial frame of \code{envir}, unless an existing binding is locked or
64
  there is no existing binding and the environment is locked.
64
  there is no existing binding and the environment is locked (when an
-
 
65
  error is signaled).
65
}
66
}
66
\references{
67
\references{
67
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
68
  Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
68
  \emph{The New S Language}.
69
  \emph{The New S Language}.
69
  Wadsworth & Brooks/Cole.
70
  Wadsworth & Brooks/Cole.
Line 75... Line 76...
75
  \code{\link{exists}},
76
  \code{\link{exists}},
76
  \code{\link{environment}}.
77
  \code{\link{environment}}.
77
}
78
}
78
\examples{
79
\examples{
79
for(i in 1:6) { #-- Create objects  'r.1', 'r.2', ... 'r.6' --
80
for(i in 1:6) { #-- Create objects  'r.1', 'r.2', ... 'r.6' --
80
 nam <- paste("r", i, sep = ".")
81
    nam <- paste("r", i, sep = ".")
81
 assign(nam, 1:i)
82
    assign(nam, 1:i)
82
}
83
}
83
ls(pattern = "^r..$")
84
ls(pattern = "^r..$")
84
 
85
 
85
##-- Global assignment within a function:
86
##-- Global assignment within a function:
86
myf <- function(x) {
87
myf <- function(x) {
87
 innerf <- function(x) assign("Global.res", x^2, envir = .GlobalEnv)
88
    innerf <- function(x) assign("Global.res", x^2, envir = .GlobalEnv)
88
 innerf(x+1)
89
    innerf(x+1)
89
}
90
}
90
myf(3)
91
myf(3)
91
Global.res # 16
92
Global.res # 16
92
 
93
 
93
a <- 1:4
94
a <- 1:4
94
assign("a[1]", 2)
95
assign("a[1]", 2)
95
a[1] == 2          #FALSE
96
a[1] == 2          # FALSE
96
get("a[1]") == 2   #TRUE
97
get("a[1]") == 2   # TRUE
97
 
98
 
98
}
99
}
99
\keyword{data}
100
\keyword{data}