[R] Lexical scoping is not what I expect

S Ellison S.Ellison at lgcgroup.com
Wed Jun 26 12:57:24 CEST 2013


 

> -----Original Message-----
> It may be helpful not to worry about the technical details, 
> just to look at the source code defining the function:  if it 
> is defined in a place where a variable can be seen, it can 
> see that variable.

I too find R's lexical scoping rules straightforward.
However, I'd say that if your code relies on lexical scoping to find something, you should probably rewrite your code.

The number of times I've seen new R users get unexpected results because they haven't noticed that their function is referencing a parent environment instead of a locally defined variable or argument is past counting.

Of course there are times when it's useful and sensible to have globally defined variables that can be accessed within a function. But they are very rare; as a default, I'd recommend avoiding it if at all possible. If your function needs something from outside, pass it as an argument.



*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list