I want to modify print() for my R6 class so that if field dt is still NULL, it print the entire Class information, as done by default. However, when dt is NOT NULL, then it only prints dt, like in example below.
How to do that?
library(R6)
Simple <- R6Class(
"Simple",
portable = F, # for convenience so that I can use either dt or self$dt in class functions
public = list(
x=1, y=2,
dt = NULL,
print = function (...) {
if (is.null(dt)){
print(...)
} else {
print(self$dt)
}
},
date="2020-10-10"
)
)
s <- Simple$new()
s
# I WANT THIS TO BE PRINTEED
# <Simple>
# Public:
# clone: function (deep = FALSE)
# date: 2020-10-10
# dt: NULL
# x: 1
# y: 2
s$dt <- mtcars
s
# WILL PRINT mtcars
When you type
sinto the console, you invoke the (S3) genericprintfunction, which dispatchesR6:::print.R6. If you look at whatR6:::print.R6does, it searches for a member function of the object called "print", and if it is present, it uses that to print the object.If a
printmethod has not been defined for the R6 object, thenR6:::print.R6will useR6:::format.R6to display the R6 object. This is what you want to see whendtisNULL.You will need to do the same within your
printmethod, since the act of defining aprintmethod within your class prevents this behaviour.Fortunately, this is straightforward:
Created on 2023-02-09 with reprex v2.0.2