Skip to content
Prev 8068 / 29559 Next

R Scoping Rules

On Sun, 18 Apr 2010, Stephen G. Eick wrote:

            
'height' is neither a local variable, nor visible in the environment where 'simple()' was defined.  That's the point of static lexical scope -- the scope depends on what was visible where the function was defined.

You seem to want dynamic scope, where variables in the calling environment are visible.  This can be faked with eval.parent(), but isn't how R naturally works.
R *does* use scoping rules like python (since python 2.1).  Your example doesn't work in python, for exactly the same reason.
...     print height
...
...     height = 10
...     simple()
...
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "<stdin>", line 3, in foo
   File "<stdin>", line 2, in simple
NameError: global name 'height' is not defined

      -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle