Lexical scoping rule in R

I am getting familiar in R and on this go I came across the concepts of lexical scoping rules in R functions. I had slightly difficult time in understanding the concepts as I came form a language that has dynamic scoping as a background. And below post (Lexical scoping rule in R) is a short summary of my understanding over lexical scoping.

Generally scoping rules determine how a value is bound to free variables in a function. Now question is what is free variables? To answer this, free variables are variables that are neither formal arguments of a function or a local variables that is initialized inside the function body.

lexical-function-free-variable

Here x , y are formal argument.

q is local argument.

z is a free variable.

With respect to R lexical scoping comes into picture when a values need to look up for variable like ‘z’ in the above function. 

Lexical scoping rule.

Search behinds from the current environment. If the value is not found then search process extents its search from current environment to its parent environment. If value is not found in the current parent environment then search is further extents to its parent environment. This process continues until empty environment is reached.

Empty environment does not have parent environment so search process stops there.

R uses lexical scoping or static scoping. In R every function, work-space, name-space, etc has some thing called environment. Every Environment is a collection of (symbol, values ) pairs. An environment may have one parent environment and more than one child environments. Only environment without a parent is called empty environment.

So when executing the above function, R uses lexical scoping rules for searching/finding the value for z. Below are two other example that illustrate the lexical scoping concepts even more precisely.

lexical-scoping-ex-1

Lexical scoping rule 2

In the above function value of ‘a’ is not defined in the current environment i.e inside the function. So search for the value ‘a’ extends to its parent environment that is global environment where it finds value for ‘a’ as 10.

 lexical-scoping-ex-2

Lexical scoping rule 1

In above slightly complicated example value of ‘a’ is not defined in the current environment i.e inside the goo function. So search for the value ‘a’ extends to its parent environment called p1 as show in the diagram. As it finds value for ‘a’ as 5. search process stops here.

 If value of ‘a’ is not found in p1 then search would have extents further to parent of p1 which is global environment in this case and value of ‘a’ would have become 10.

That’s it! Lexical scoping is as simple as it is. 
I doubt DM community says R data processing limit to size of physical memory. Is this limitation due to lexical scoping, let know your thoughts in the comments.


9 Comments

  1. d wrote
    at 8:09 AM - 6th May 2014 Permalink

    Hi Shakthy, You have beautifully represented the scoping! I read many pages and trying to understand lexical scoping. But I stop reading now because your presentation of the concept is very clear

  2. shakthydoss wrote
    at 8:22 AM - 6th May 2014 Permalink

    Thanks. I’m glad to help you.

  3. Ashim Lamichhane wrote
    at 5:16 PM - 3rd July 2014 Permalink

    Great Work Man!!! Cheers!!!

  4. shakthydoss wrote
    at 5:22 PM - 3rd July 2014 Permalink

    Thank you Ashim.

  5. Ashim Lamichhane wrote
    at 11:46 AM - 3rd July 2014 Permalink

    Great Work Man!!! Cheers!!!

  6. shakthydoss wrote
    at 11:52 AM - 3rd July 2014 Permalink

    Thank you Ashim.

  7. David Klemitz wrote
    at 3:29 AM - 23rd January 2015 Permalink

    Very clear and to the point ! Thank you.

  8. shakthydoss wrote
    at 7:24 AM - 23rd January 2015 Permalink

    Thanks David.

  9. Igal Israilevich wrote
    at 9:43 PM - 17th February 2015 Permalink

    I join to others, that was great explanation

Post a Comment

Your email is never published nor shared. Required fields are marked *