[R] Communicating from one function to another

Alberto Monteiro albmont at centroin.com.br
Mon Nov 26 19:46:52 CET 2007


Duncan Murdoch wrote:
> 
> R doesn't really have global variables.  <<- goes looking in parent 
> environments until it finds the target variable, and makes the 
> assignment there.  If it never finds one, it makes the assignment in 
> the "global environment", but the name is misleading:  it should 
> really be called the "user workspace".
> 
Got it.

So, in this case:

x <- 1

f <- function() {
  x <- 2
  g <- function() {
    x <<- 3
  }
  g()
  x
}

f()

It's expected that f will return 3, but x would still be 1?

Alberto Monteiro



More information about the R-help mailing list