I am using the R6 package for OO programming in a package I am developing. My R6 object stores a data frame as well as other information. I would like to override the data.frame() function when called on my R6 object so that its stored data frame is returned.
Is this possible?
For example:
library(R6)
myobj <- R6Class("myobj", list(
df = NULL,
args = NULL,
initialize = function(df, args=NULL) {
self$df <- df
self$args <- args
},
print = function(...) {
cat("myobj: \n")
cat(" df: ", nrow(self$df), " x ", ncol(self$df), "\n")
invisible(self)
},
data.frame = function(...) {
self$df
}
))
print(myobj) # works
df <- data.frame(myobj) # does not work
I've read the R6 documentation, I guess I'm confused how we can override print but not other functions called on R6 objects.