Object-Oriented Programming with S3 and R6 in R
Richie Cotton
Data Evangelist at DataCamp
thing_factory <- R6Class(
"Thing"
)
child_thing_factory <- R6Class(
"ChildThing",
inherit = thing_factory
)
grand_child_thing_factory <- R6Class(
"GrandChildThing",
inherit = child_thing_factory
)
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") } )
)
grand_child_thing_factory <- R6Class( "GrandChildThing", inherit = child_thing_factory, public = list( do_something = function() { message("the grand-child do_something method")
super$do_something()
super$super$do_something()
} ) )
a_grand_child_thing <- grand_child_thing_factory$new()
a_grand_child_thing$do_something()
the grand-child do_something method
the child do_something method
Error in a_grand_child_thing$do_something(): attempt to apply non-function
child_thing_factory <- R6Class( "ChildThing", inherit = thing_factory, public = list( do_something = function() { message("the child do_something method") } ),
active = list( super_ = function() super )
)
grand_child_thing_factory <- R6Class( "GrandChildThing", inherit = child_thing_factory, public = list( do_something = function() { message("the grand-child do_something method")
super$do_something()
super$super_$do_something()
} ) )
a_grand_child_thing <- grand_child_thing_factory$new()
a_grand_child_thing$do_something()
the grand-child do_something method
the child do_something method
the parent do_something method
super_
super_
should simply return super
Object-Oriented Programming with S3 and R6 in R