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
),
public = list(
do_something = function(x, y, z) {
# do something here
}
)
)
child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory
public = list( do_something_else = function() { # more functionality } )
)
a_thing <- thing_factory$new()
class(a_thing)
"Thing" "R6"
inherits(a_thing, "Thing")
TRUE
inherits(a_thing, "R6")
TRUE
a_child_thing <- child_thing_factory$new()
class(a_child_thing)
"ChildThing" "Thing" "R6"
inherits(a_child_thing, "ChildThing")
TRUE
inherits(a_child_thing, "Thing")
TRUE
inherits(a_child_thing, "R6")
TRUE
inherit
arg to R6Class()
Object-Oriented Programming with S3 and R6 in R