Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Lexical Scoping vs Dynamic Scoping in R Programming

  • Last Updated : 03 Sep, 2021

R is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge tool. Scoping Rules of a language are responsible for determining how value will be associated with the free variable in a function in the R language. 

Lexical Scoping

In Lexical Scoping the scope of the variable is determined by the textual structure of a program. Most programming languages we use today are lexically scoped. Even, a human can determine the scope of a variable just by properly reading the code. Below is a code of lexical Scoping in R. 

Python3




# R program to depict scoping
 
# Assign a value to a
a <- 1
 
# Defining function b and c
b <- function() a
c <- function(){
  a <- 2
  b()
}
 
# Call to function c
c()

Output: 

 1 

In this example, first, a mapping for a is created. In the next line a function b is defined which returns the value of some a. On line 3, we define a function c, which creates a new mapping for a and then calls b. Note that the assignment on line 4 does not update the definition on line 1 and in the Output value, 1 is returned. 



Dynamic Scoping

In Dynamic scoping, the variable takes the value of the most latest value assigned to that variable 
Here, Considering the same above example.  

Python3




# R program to depict scoping
 
# Assign a value to a
a <- 1
 
# Defining function b and c
b <- function() a
c <- function(){
  a <- 2
  b()
}
 
# Call to function c
c()

Output: 

 1 

The output of this code should be 2 according to the rules of Dynamic as the latest value assigned to the variable a is 2. 

Scope in R Language

Sometimes, in R Language code can give the appearance of dynamic Scoping which happens when a function is defined in the global environment and also called from the global environment, resulting in the same environment for defining and calling the environment which can be depicted in the example given below. 

Python3




# R program to depict scoping
 
# Assign a value to a
a <- 10
 
# Defining function f
f <- function(x) {
  a <- 2
  a ^ 2 + g(x)
}
 
# Defining function g
g <- function(x) {
  x * a
}
 
# Call to function f
f(3)

Output: 

 34 

In this example, g is looked up in the environment in which it is defined and hence the value of a is 10. With dynamic scoping, the value of a variable is looked up in the environment in which it is called. In R that environment is known as the parent environment so in function f the value of a will be 2 whereas in g the value will be 10. This may create an illusion of R language being a dynamic language but in reality, it turns out to be Lexical Scope language. Below are some differences between the lexical and dynamic scoping in R programming.

Difference Between Lexical and Dynamic Scoping

LexicalDynamic
In this variable refers to top level environmentIn this variable is associated to most recent environment
It is easy to find the scope by reading the codeIn this programmer has to anticipate all possible contexts
It is dependent on how code is writtenIt is dependent on how code is executed
Structure of program defines which variable is referred to.Runtime state of program stack determines the variable.
It is property of program text and unrelated to real time stack.It is dependent on real time stack rather than program text
It provides less flexibilityIt provides more flexibility
Accessing to nonlocal variables in lexical scoping is fast.Accessing to nonlocal variables in dynamic scoping takes more time.
Local variables can be protected to be accessed by local variables.There is no way to protect local variables to be accessed by subprograms.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :