0
0
R Programmingprogramming~10 mins

Environment and closures in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Environment and closures
Define function with free variables
Function remembers environment
Call function
Look up variables in saved environment
Execute function body using saved variables
Return result
A closure is a function that remembers the environment where it was created, so it can access variables even after that environment is gone.
Execution Sample
R Programming
make_counter <- function() {
  count <- 0
  function() {
    count <<- count + 1
    count
  }
}
counter <- make_counter()
counter()
This code creates a counter function that remembers and updates a variable 'count' each time it is called.
Execution Table
StepActionEnvironment StateVariable 'count'Output
1Call make_counter()Create new environment for make_countercount = 0Function returned (closure)
2Assign returned function to 'counter'Closure remembers environment with count=0count = 0No output
3Call counter()Use saved environmentcount = 0 -> count = 11
4Call counter() againUse saved environmentcount = 1 -> count = 22
5Call counter() againUse saved environmentcount = 2 -> count = 33
💡 Execution stops after calls to counter() show count increasing, demonstrating closure remembers state.
Variable Tracker
VariableStartAfter call 1After call 2After call 3
count0123
Key Moments - 2 Insights
Why does 'count' keep its value between calls to 'counter()'?
Because 'counter' is a closure that remembers the environment where 'count' was defined, so it updates the same 'count' variable each time (see execution_table steps 3-5).
What does the '<<-' operator do in the function?
It updates the 'count' variable in the parent environment of the function, not a new local variable, allowing the closure to modify 'count' (see execution_table step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'count' after calling counter() the first time?
ANA
B1
C0
DFunction itself
💡 Hint
Check the 'Variable count' column at step 3 in the execution_table.
At which step does the 'count' variable become 2?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Variable count' column in the execution_table rows.
If we remove '<<-' and use '<-' inside the inner function, what happens to 'count'?
A'count' becomes local and does not update parent
B'count' in parent environment updates
CError occurs
DFunction stops working
💡 Hint
Recall that '<<-' updates parent environment variables, '<-' creates local variables (see key_moments question 2).
Concept Snapshot
Environment and closures in R:
- Functions remember the environment where they were created.
- Closures can access and modify variables from that environment.
- Use '<<-' to update variables in the parent environment.
- This allows functions to keep state between calls.
Full Transcript
This example shows how R functions can remember variables from their creation environment, called closures. The make_counter function creates a variable 'count' and returns a function that increases 'count' each time it is called. The returned function remembers 'count' even after make_counter finishes. The '<<-' operator updates 'count' in the saved environment, so the value persists between calls. This is why calling counter() repeatedly increases 'count' from 0 to 1, then 2, then 3. Closures let functions keep state, like a counter, by saving their environment.