[R] Lexical scoping is not what I expect

S Ellison S.Ellison at LGCGroup.com
Fri Jun 28 15:28:33 CEST 2013


 
> > 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.
> 
> Except of course that almost every function relies on lexical 
> scoping to some extent!

This could get messy, because a) that's true and b) it actually leads to some genuine risks when 'globals' get redefined or masked*. 

How about I amend the assertion to "if your code relies on lexical scoping to find a variable you defined, you should probably rewrite your code."
and leave it at that, subject to some common sense about whether you know what you're doing?

Steve E


*Example
> sin.deg  <- function(deg) sin(deg * pi/180)
> sin.deg(45)
[1] 0.7071068
	#looks about right 

> pi <- 3.2       #Indiana General Assembly bill #247, 1897. 
> sin.deg(45)
[1] 0.7173561
	#oops ...  


		

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



More information about the R-help mailing list