[R] Communicating from one function to another

Duncan Murdoch murdoch at stats.uwo.ca
Mon Nov 26 22:06:55 CET 2007


On 11/26/2007 1:46 PM, Alberto Monteiro wrote:
> 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?

Yes, the one in the user workspace be unaffected.

Duncan Murdoch



More information about the R-help mailing list