[R] understanding lexical scope

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Fri Dec 19 19:15:58 CET 2008


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of 
> joseph.g.boyer at gsk.com
> Sent: Friday, December 19, 2008 7:41 AM
> To: Thomas Lumley
> Cc: r-help at r-project.org
> Subject: Re: [R] understanding lexical scope
> 
> Thomas, Jeff, Mark, Antonio,
> 
> Thank you for your answers. They have helped me clarify how R 
> functions 
> work. They work differently from SAS functions (which SAS 
> calls macros.)

Well, SAS macros are not functions in the traditional sense.  The SAS macro language for the most part just does text substitution prior to the SAS code being sent to the SAS "compiler"/interpreter. So, your description of rewriting the "function body" in step 1. below, is fairly accurate for SAS macro, but it is not accurate for R.  If you try to fit R functions into a SAS macro language mold you will only confuse yourself on both accounts.  I will leave the technical details of R functions to the R experts.

> 
> If you know SAS, consider the following code:
> 
> *********************
> %macro q(y);
> 
> data one;
> outvar = &y. + &x.; output;
> call symputx("outvar", outvar, "G");
> run;
> 
> %mend;
> 
> %macro w(x);
> 
> %q(&x.);
> %put &outvar.;
> 
> %mend;
> **************
> 
> Then %w(2); will result in the value 4 being placed in the SAS log.
> 
> 
> To me, while the coding is quite awkward, the execution is 
> logical. The 
> variable x has been defined by the call to the macro w, so 
> there is no 
> problem when SAS encounters a reference to x in the macro q.
> 
> But in the equivalent code in R, 
> 
> q <- function(y) y +x; w <- function(x) q(x); w(2);
> 
> when R can't find the second argument of q in the local 
> environment of the 
> macro q, it doesn't look in the local environment of the 
> macro w, it goes 

If you want to try to compare the R language to SAS language (not favorable to SAS for most on this list), the better comparison for understanding is the data step language, not SAS macro.

> all the way back to 
> the global environment, as you have all pointed out.
> 
> So in my little model of how R functions work, when a 
> function is called
> 
> 1. R rewrites the body of the function, replacing all of the 
> parameter 
> names with the values given to them in the function call.
> 
> 2. R then tries to execute the expressions. But R only 
> "remembers" the 
> assignment of values to parameter names during step 1. Thus 
> in our example
>         it has to go the global environment to find a value for "x" 
> referenced in q.
> 
> Is this right?
> 
> 
> 
I bet one of the expeRts on the list will provide you with more detail than could have ever hoped for.

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA  98504-5204
 
 



More information about the R-help mailing list