R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Lexical Scoping vs Dynamic Scoping in R

In programming languages, variable scoping determines how the value of a variable is looked up. There are two main scoping rules used across different languages: lexical (or static) scoping and dynamic scoping. R uses lexical scoping, but it's beneficial to understand both to grasp the broader concept.

1. Lexical (Static) Scoping:

  • Definition: In lexical scoping, the value of a free variable (i.e., a variable used in a function but not defined in it) is searched in the environment where the function was defined.
  • R's Behavior: R uses lexical scoping. For a free variable in a function, R first checks within the function itself, then the environment where the function was defined, followed by its parent environment, and so on, until it reaches the global environment.
  • Advantages:
    • Predictability: You can determine variable values by looking at the program's structure.
    • Encapsulation: Helps in building modular code where details are hidden in local scopes.
    • Enables closures: Functions that carry information from their defining environment.

2. Dynamic Scoping:

  • Definition: Under dynamic scoping, the value of a free variable is searched in the environment from which the function was called (i.e., the calling environment), not where it was defined.
  • R's Behavior: R does not use dynamic scoping by default. However, certain functions like assign() with the inherits option can mimic dynamic scoping behavior.
  • Advantages:
    • Flexibility: Allows for different behavior depending on the calling context.
    • Can simplify certain problems where the calling context is crucial.

Comparing Lexical vs. Dynamic Scoping:

Consider the following pseudo-code:

x = 10

function f() {
    print(x)
}

function g() {
    x = 5
    f()
}

g()

Under Lexical Scoping: The print statement in f() would show 10 because f would use the value of x from the environment where it was defined, which is the global environment.

Under Dynamic Scoping: The print statement in f() would show 5 because f would use the value of x from the environment where it was called, which is inside function g.

Conclusion:

While R uses lexical scoping by default, understanding both lexical and dynamic scoping concepts is valuable. Lexical scoping, as seen in R, offers predictability and modularity. Dynamic scoping, although not native to R, provides flexibility based on the calling environment, but it can make code harder to reason about and debug.

  1. R lexical scoping examples:

    • Overview: Offer practical examples demonstrating lexical scoping in R.

    • Code:

      # Lexical scoping example
      outer_function <- function(x) {
        inner_function <- function(y) {
          x + y
        }
        inner_function
      }
      
      # Create closures with different values of x
      closure1 <- outer_function(5)
      closure2 <- outer_function(10)
      
      # Call closures with different y values
      result1 <- closure1(3)  # Output: 8
      result2 <- closure2(3)  # Output: 13