A possible way is to use the dot-dot-dot (ellipsis) argument for a constructor that allows you to pass an arbitrary number of arguments to a function. After that, you need to convert "..." to a list and parse the input arguments.
For example, suppose we want to overload the constructor by arity.
myClass <- R6Class("myClass", public = list( name = NA, age = NA, favouriteFood = NA, # Constructor overloaded by arity initialize = function(...) { arguments <- list(...) switch(nargs(), # one input argument {self$name <- arguments[[1]]}, # two input arguments {self$name <- arguments[[1]] self$age <- arguments[[2]]}, # three input arguments {self$name <- arguments[[1]] self$age <- arguments[[2]] self$favouriteFood <- arguments[[3]]} ) print(self) }) )
The constructor call now gives us
> a = myClass$new("john") <myClass> Public: age: NA clone: function (deep = FALSE) favouriteFood: NA initialize: function (...) name: john > a = myClass$new("john",35) <myClass> Public: age: 35 clone: function (deep = FALSE) favouriteFood: NA initialize: function (...) name: john > a = myClass$new("john",35,"pasta") <myClass> Public: age: 35 clone: function (deep = FALSE) favouriteFood: pasta initialize: function (...) name: john
source share