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
In R, lexical scoping (also known as static scoping) is a fundamental concept that dictates how the values of free variables in a function are resolved. In simpler terms, lexical scoping determines how R looks up variable values when they are called inside a function.
Local vs. Free Variables:
How R Resolves Free Variables: With lexical scoping, R looks for free variable values in the following order:
Let's look at an example:
x <- 5 example_function <- function() { y <- 3 return(x + y) } example_function()
Here, y
is a local variable, and x
is a free variable. When example_function
is called, R will look for x
in the global environment because that's where the function was defined.
The result of example_function()
will be 8
.
Encapsulation and Information Hiding: By relying on the environment where functions are defined, you can encapsulate details and create more modular and maintainable code.
Closures: Lexical scoping allows for the creation of closures, which are functions generated by other functions and that have access to the environment of the parent function. This feature can be powerful for creating specialized functions on-the-fly.
make_multiplier <- function(multiplier) { function(number) { return(multiplier * number) } } double <- make_multiplier(2) print(double(5)) # Returns 10
In this example, make_multiplier
returns a function that multiplies a number by a specific multiplier. When we call make_multiplier(2)
, it returns a function that doubles any number passed to it.
Understanding lexical scoping in R is crucial for effectively using the language, especially when defining and using functions. It allows for more structured, encapsulated, and flexible code by dictating how variable values are determined within functions.
Lexical scoping in R with examples:
Overview: Provide practical examples to illustrate the application of lexical scoping in R.
Code:
# Example of lexical scoping 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
Closures and lexical scoping in R:
Overview: Explain how closures, as a result of lexical scoping, work in R.
Code:
# Example of closure and lexical scoping create_closure <- function(x) { function(y) { x + y } } # Create closures with different values of x closure1 <- create_closure(5) closure2 <- create_closure(10) # Call closures with different y values result1 <- closure1(3) # Output: 8 result2 <- closure2(3) # Output: 13