0
0
R Programmingprogramming~20 mins

Environment and closures in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Environment and Closures Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a closure capturing a variable
What is the output of this R code?
make_counter <- function() {
  count <- 0
  function() {
    count <<- count + 1
    count
  }
}
c <- make_counter()
c()
c()
R Programming
make_counter <- function() {
  count <- 0
  function() {
    count <<- count + 1
    count
  }
}
c <- make_counter()
c()
c()
A
[1] 2
[1] 3
B
[1] 0
[1] 1
C
[1] 1
[1] 1
D
[1] 1
[1] 2
Attempts:
2 left
💡 Hint
Think about how the <<- operator changes the variable in the parent environment.
Predict Output
intermediate
2:00remaining
Value of variable after nested function calls
What is the value of x after running this code?
x <- 5
f <- function() {
  x <- 10
  g <- function() {
    x <<- 20
  }
  g()
  x
}
f()
x
R Programming
x <- 5
f <- function() {
  x <- 10
  g <- function() {
    x <<- 20
  }
  g()
  x
}
f()
x
A5
B10
C20
DError
Attempts:
2 left
💡 Hint
The <<- operator modifies the variable in the nearest parent environment where it exists.
🔧 Debug
advanced
3:00remaining
Why does this closure not remember the updated value?
Consider this code:
make_funcs <- function() {
  funcs <- list()
  for (i in 1:3) {
    funcs[[i]] <- function() i
  }
  funcs
}
fs <- make_funcs()
fs[[1]]()
fs[[2]]()
fs[[3]]()

What is the output of the three calls to fs[[1]](), fs[[2]](), and fs[[3]]()?
R Programming
make_funcs <- function() {
  funcs <- list()
  for (i in 1:3) {
    funcs[[i]] <- function() i
  }
  funcs
}
fs <- make_funcs()
fs[[1]]()
fs[[2]]()
fs[[3]]()
A
[1] 1
[1] 2
[1] 3
B
[1] 3
[1] 3
[1] 3
C
[1] 1
[1] 1
[1] 1
DError in function calls
Attempts:
2 left
💡 Hint
Think about when the variable i is looked up by the functions.
📝 Syntax
advanced
2:00remaining
Identify the error in this closure code
What error does this code produce?
make_adder <- function(x) {
  function(y) {
    x + y
  }
}
add5 <- make_adder(5)
add5("3")
R Programming
make_adder <- function(x) {
  function(y) {
    x + y
  }
}
add5 <- make_adder(5)
add5("3")
AError: non-numeric argument to binary operator
BError: object 'y' not found
CNo error, returns 8
DError: unexpected string constant
Attempts:
2 left
💡 Hint
Check the types of the arguments used in the addition.
🚀 Application
expert
3:00remaining
How many unique environments are created?
Consider this code:
make_funcs <- function() {
  funcs <- list()
  for (i in 1:3) {
    funcs[[i]] <- local({
      x <- i
      function() x
    })
  }
  funcs
}
fs <- make_funcs()
length(unique(sapply(fs, environment)))

What is the output of the last line?
R Programming
make_funcs <- function() {
  funcs <- list()
  for (i in 1:3) {
    funcs[[i]] <- local({
      x <- i
      function() x
    })
  }
  funcs
}
fs <- make_funcs()
length(unique(sapply(fs, environment)))
A3
B1
C0
DError
Attempts:
2 left
💡 Hint
Each call to local creates a new environment for the function.