Object-Oriented Programming with S3 and R6 in R
Richie Cotton
Data Evangelist at DataCamp
thing_factory <- R6Class(
"Thing",
public = list(
do_something = function() {
message("the parent do_something method")
}
)
)
child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory,
public = list( do_something = function() { message("the child do_something method") },
do_something_else = function() { message("the child do_something_else method") }
)
)
a_child_thing <- child_thing_factory$new()
a_child_thing$do_something()
the child do_something method
private$
accesses private fields
self$
accesses public methods in self
super$
accesses public methods in parent
child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory, public = list( do_something = function() { message("the child do_something method") }, do_something_else = function() { message("the child do_something_else method")
self$do_something()
super$do_something()
} ) )
a_child_thing <- child_thing_factory$new()
a_child_thing$do_something_else()
the child do_something_else method
the child do_something method
the parent do_something method
self$
accesses public methods in selfsuper$
accesses public methods in parentObject-Oriented Programming with S3 and R6 in R