Skip to content
Prev 325988 / 398502 Next

Lexical scoping is not what I expect

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}}

Thread (27 messages)

David Kulp Lexical scoping is not what I expect Jun 24 Rui Barradas Lexical scoping is not what I expect Jun 24 Gabor Grothendieck Lexical scoping is not what I expect Jun 24 arun Lexical scoping is not what I expect Jun 24 Rolf Turner Lexical scoping is not what I expect Jun 24 Marc Schwartz Lexical scoping is not what I expect Jun 24 Duncan Murdoch Lexical scoping is not what I expect Jun 24 David Kulp Lexical scoping is not what I expect Jun 24 William Dunlap Lexical scoping is not what I expect Jun 24 Duncan Murdoch Lexical scoping is not what I expect Jun 25 S Ellison Lexical scoping is not what I expect Jun 26 Duncan Murdoch Lexical scoping is not what I expect Jun 26 Hadley Wickham Lexical scoping is not what I expect Jun 27 S Ellison Lexical scoping is not what I expect Jun 28 Duncan Murdoch Lexical scoping is not what I expect Jun 28 John Fox Lexical scoping is not what I expect Jun 28 Duncan Murdoch Lexical scoping is not what I expect Jun 28 Brian Ripley Lexical scoping is not what I expect Jun 28 John Fox Lexical scoping is not what I expect Jun 28 John Fox Lexical scoping is not what I expect Jun 28 Rolf Turner Lexical scoping is not what I expect Jun 28 Yihui Xie Lexical scoping is not what I expect Jun 28 William Dunlap Lexical scoping is not what I expect Jun 29 Greg Snow Lexical scoping is not what I expect Jun 29 Rolf Turner Lexical scoping is not what I expect Jun 30 Duncan Murdoch Lexical scoping is not what I expect Jun 30 David Winsemius Lexical scoping is not what I expect Jun 30