Object-Oriented Programming with S3 and R6 in R
Richie Cotton
Data Evangelist at DataCamp
thing_factory <- R6Class( "Thing", private = list( ..a_field = "a value", ..another_field = 123 ), active = list(
a_field = function() {
if(is.na(private$..a_field)) { return("a missing value") }
private$..a_field
},
another_field = function(value) {
if(missing(value)) { private$..another_field } else {
assert_is_a_number(value)
private$..another_field <- value }
}
) )
a_thing <- thing_factory$new()
a_thing$a_field
"a value"
a_thing$a_field <- "a new value"
Error in (function (value) : a_field is read-only.
a_thing$another_field <- 456
a_thing$another_field <- "456"
Error in (function (value) : is_a_number :
value is not of class 'numeric'; it has class 'character'.
Object-Oriented Programming with S3 and R6 in R