Complete the code to create a new R6 class named 'Person'.
Person <- R6Class([1] = "Person")
The argument to R6Class() to name the class is classname.
Complete the code to add a public method 'greet' that prints 'Hello!'.
Person <- R6Class(classname = "Person", public = list( greet = function() { print([1]) } ) )
The print function requires a string in quotes. Double quotes around 'Hello!' are correct.
Fix the error in the code to correctly initialize a field 'name' with a default value 'John'.
Person <- R6Class(classname = "Person", public = list( name = [1], initialize = function(name = "John") { self$name <- name } ) )
Fields should be initialized as NULL or a default value. Setting to NULL avoids errors before initialization.
Fill both blanks to add a private field 'age' and a public method 'get_age' that returns it.
Person <- R6Class(classname = "Person", private = list( [1] = NULL ), public = list( get_age = function() { return(private$[2]) } ) )
The private field is named 'age', and the public method accesses it as 'private$age'.
Fill all three blanks to create a method 'set_name' that updates the 'name' field and prints confirmation.
Person <- R6Class(classname = "Person", public = list( name = NULL, set_name = function(new_name) { self$[1] <- [2] print(paste("Name set to", [3])) } ) )
The method sets self$name to new_name and prints it.