Variables can be assigned globally from any environment using <<-
. bar()
can now access y
.
bar <- function() {
z <- x + y
return(z)
}
foo <- function() {
y <<- 3
z <- bar()
return(z)
}
foo()
4
Global assignment is highly discouraged. Use of a wrapper function or explicitly calling variables from another local environment is greatly preferred.