0
0
R Programmingprogramming~20 mins

R6 classes in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
R6 Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of method call in R6 class
What is the output of the following R code?
R Programming
library(R6)
Person <- R6Class("Person",
  public = list(
    name = NULL,
    initialize = function(name) {
      self$name <- name
    },
    greet = function() {
      paste("Hello, my name is", self$name)
    }
  )
)
p <- Person$new("Alice")
p$greet()
AError: object 'name' not found
B"Hello, my name is NULL"
C"Hello, my name is Alice"
DNULL
Attempts:
2 left
💡 Hint
Check how the initialize method sets the name and how greet accesses it.
🧠 Conceptual
intermediate
1:30remaining
Understanding private fields in R6
Which statement about private fields in R6 classes is TRUE?
APrivate fields are global variables shared by all instances of the class.
BPrivate fields can be accessed directly from outside the object using $ operator.
CPrivate fields are automatically copied to public fields when the object is created.
DPrivate fields are defined inside the private list and cannot be accessed directly from outside the object.
Attempts:
2 left
💡 Hint
Think about encapsulation and how private fields protect data.
🔧 Debug
advanced
2:00remaining
Identify the error in R6 method definition
What error will this code produce when run?
R Programming
library(R6)
Counter <- R6Class("Counter",
  public = list(
    count = 0,
    increment = function() {
      count <- count + 1
    }
  )
)
c <- Counter$new()
c$increment()
c$count
A0
B1
CError: object 'count' not found
DError: attempt to modify a locked binding
Attempts:
2 left
💡 Hint
Check how the increment method modifies the count variable.
📝 Syntax
advanced
2:30remaining
Correct syntax for defining an active binding in R6
Which option correctly defines an active binding named 'double' that returns twice the value of private$x?
A
active = list(
  double = function(value) {
    if (missing(value)) private$x * 2 else private$x &lt;- value / 2
  }
)
B
active = list(
  double = function() {
    private$x * 2
  }
)
C
active = list(
  double = function(value) {
    private$x * 2
  }
)
D
active = list(
  double = function(value) {
    if (!missing(value)) private$x * 2 else private$x &lt;- value / 2
  }
)
Attempts:
2 left
💡 Hint
Active bindings use a function with an optional argument to get or set values.
🚀 Application
expert
3:00remaining
Predict the number of items in a list after method calls
Consider this R6 class that stores unique names in a list. After running the code below, how many items are in the list stored in the object?
R Programming
library(R6)
UniqueNames <- R6Class("UniqueNames",
  private = list(
    names = list()
  ),
  public = list(
    add_name = function(name) {
      if (!(name %in% private$names)) {
        private$names <- c(private$names, name)
      }
    },
    get_names = function() {
      private$names
    }
  )
)
obj <- UniqueNames$new()
obj$add_name("Anna")
obj$add_name("Bob")
obj$add_name("Anna")
obj$add_name("Cara")
length(obj$get_names())
AError: object of type 'closure' is not subsettable
B3
C4
D2
Attempts:
2 left
💡 Hint
Check how the add_name method checks for duplicates before adding.