0
0
R Programmingprogramming~5 mins

Environment and closures in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an environment in R?
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
What is a closure in R?
AA special kind of loop
BA type of variable
CA data frame
DA function that remembers its environment
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
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
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
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.