An environment in R is like a box that stores variables and their values. It keeps track of where to find variables when you use them in your code.
Click to reveal answer
beginner
What is a closure in R?
A closure is a function that remembers the environment where it was created. It can use variables from that environment even after the environment is no longer active.
Click to reveal answer
intermediate
How does R find a variable inside nested functions?
R looks for the variable in the current environment first. If it is not found, it checks the parent environment, and keeps going up until it finds the variable or reaches the empty environment.
Click to reveal answer
intermediate
Explain why closures are useful with an example.
Closures let you create functions that keep some data inside. For example, a function that adds a fixed number to its input can remember that number inside its environment.
Click to reveal answer
advanced
What happens if you change a variable inside a closure's environment?
Changing a variable inside a closure's environment updates the stored value. The closure will use the new value the next time it runs.
Click to reveal answer
What does an environment in R store?
AOnly numbers
BOnly functions
CVariables and their values
DOnly text strings
✗ Incorrect
An environment stores variables and their values so R knows where to find them.
What is a closure in R?
AA special kind of loop
BA type of variable
CA data frame
DA function that remembers its environment
✗ Incorrect
A closure is a function that keeps access to the environment where it was created.
If a variable is not found in the current environment, where does R look next?
ARandom environment
BParent environment
CGlobal environment only
DIt stops searching
✗ Incorrect
R searches the parent environment next, moving up until it finds the variable or reaches the global environment.
Why are closures useful?
AThey keep data inside functions for later use
BThey speed up code execution
CThey delete variables automatically
DThey create new environments
✗ Incorrect
Closures keep data inside functions, allowing the function to remember values between calls.
What happens when you change a variable inside a closure's environment?
AThe closure uses the updated value next time
BThe closure breaks
CThe variable resets to original
DNothing changes
✗ Incorrect
Changing the variable updates the stored value, so the closure uses the new value.
Describe what an environment is in R and how it helps with variable lookup.
Think of it as a box where R keeps your variables.
You got /3 concepts.
Explain closures in R and why they are useful with a simple example.
Imagine a function that remembers a secret number to add.
You got /3 concepts.
Practice
(1/5)
1. What is an environment in R programming?
easy
A. A type of loop for repeating code
B. A place where variables and their values are stored
C. A function that takes another function as input
D. A package that extends R's capabilities
Solution
Step 1: Understand the definition of environment
In R, an environment is a collection where variables and their values are stored together.
Step 2: Differentiate from other options
Options B, C, and D describe loops, higher-order functions, and packages, which are not environments.
Final Answer:
A place where variables and their values are stored -> Option B
Quick Check:
Environment = variable storage [OK]
Hint: Remember: environment stores variables like a box [OK]
Common Mistakes:
Confusing environment with a function
Thinking environment is a loop
Mixing environment with packages
2. Which of the following is the correct way to create a closure in R?
easy
A. f <- function(x) { function() x + 1 }
B. f <- function(x) { x + 1 }
C. f <- function(x) { x <- x + 1 }
D. f <- function(x) { print(x) }
Solution
Step 1: Identify closure structure
A closure is a function that returns another function which remembers variables from its environment.
Step 2: Check each option
f <- function(x) { function() x + 1 } returns a function that uses 'x' from the outer function, creating a closure. Others do not return a function.
Final Answer:
f <- function(x) { function() x + 1 } -> Option A
Quick Check:
Closure returns function using outer variable [OK]
Hint: Closure = function returning function using outer variables [OK]
Common Mistakes:
Not returning a function inside another function
Modifying variable without returning a function
Confusing print with returning a function
3. What will be the output of the following R code?
The inner function modifies 'count' in the parent environment using <<- and increments it by 1 each call.
Step 2: Trace the calls to counter()
First call returns 1 (0+1), second call returns 2 (1+1).
Final Answer:
[1] 1 followed by [1] 2 -> Option D
Quick Check:
Closure increments count with <<- [OK]
Hint: <<- changes outer variable, so count increases each call [OK]
Common Mistakes:
Thinking count resets each call
Confusing <- with <<- operator
Expecting error due to variable scope
4. Identify the error in this R code that tries to create a closure:
make_adder <- function(x) {
function(y) {
x + y
}
}
add5 <- make_adder(5)
add5(3)
rm(x)
medium
A. The function make_adder is missing a return statement
B. Removing 'x' breaks the closure, causing an error on add5(3)
C. No error; code runs fine
D. The inner function should use <<- to modify x
Solution
Step 1: Understand closure variable dependency
The inner function depends on 'x' stored in its own dedicated environment created by make_adder.
Step 2: Effect of removing 'x'
rm(x) removes 'x' from the global environment if it exists, but the closure's environment retains its own 'x' = 5, so add5(3) works fine.
Final Answer:
No error; code runs fine -> Option C
Quick Check:
Closure environments are independent of global [OK]
Hint: rm(x) doesn't affect closure's private x [OK]
Common Mistakes:
Thinking functions need explicit return
Believing <<- is needed to read outer variables
Mistakenly thinking rm(x) removes closure's x
5. You want to create a function in R that remembers a secret number and returns a function to check guesses. Which approach correctly uses closures to keep the secret safe?
secret_keeper <- function(secret) { function(guess) { guess == secret } } returns a function that compares guess to secret stored in the closure environment, keeping secret safe.