Working with R6
An enhanced version of RC is R6, a package that implements a more efficient reference class that supports public and private fields and methods, and some other powerful features.
Run the following code to install the package:
install.packages("R6")
The R6 class allows us to define classes that are even more like popular object-oriented programming languages. The following code is an example where we define the Vehicle
class. It has some public fields and methods for users and some private fields and methods for internal use:
library(R6) Vehicle <- R6Class("Vehicle", public = list( name = NA, model = NA, initialize = function(name, model) { if (!missing(name)) self$name <- name if (!missing(model)) self$model <- model }, move = function(movement) { private$start() private$position <- private$position + movement private$stop() ...