I'd like to be able to redefine a public method in my R6 class (so that it changes according to the type of data that the class holds) - like below
library(R6)
Simple <- R6Class(
"Simple",
public = list(
dt = mtcars,
my.print = function (){
print(self$dt$mpg)
}
)
)
s <- Simple$new()
s$my.print()
s$dt <- iris
s$my.print() <- function() {
print(self$dt$Species)
}
The code above gives error:
Error in s$my.print() <- function() { :
invalid (NULL) left side of assignment
Alternatively, i know I can do this:
Simple$set("public", "my.print2", function {
print(self$dt$Species) } )
But this is also not suitable as then I would have to reinitialize my classes every time I do this.
Is there a proper way to so that?