I think that the way it is currently structured there is an enumerated hardcoded list of types for which the print functions are overridden each time a call is evaluated from the notebook. There does not appear to be a way to add a type to be handled specially. It looks like the route is to create a function that returns TRUE
when evaluated in Rstudio and FALSE
otherwise. This is the hack that I came up with.
rs_has_overrides <- function(){
length(as.environment("tools:rstudio")$.rs.S3Overrides) > 0L
}
Which so far works. This as a conditional in my print.*
function to then create and print the corresponding html should get me what I need.
As an aside I will mention that a more elegant and less error prone solution might be to add a function that handles all printing for rstudio, similar to what is done in knitr with knit_print
.
rstudio_print <- function(x, ...)UseMethod("rstudio_print")
rstudio_print.default <- function(x, ...)UseMethod("print")
rstudio_print.myclass <- function(x, ...){
# the customized methods would actually call the functions to register
# the html output, widgets, plots, etc. to be incorporated into the
# Rmd Notebook.
base::print(div("printing from ", code("rstudio_print"), "function."))
invisible(x)
}
Even better one could override the base print method with something like
print <- function(x, ...){
method <- getOption("print.method", "print")
UseMethod(method, x)
}
Then those that capture and direct output to other places could set
options(print.method = "rstudio_print")
Then if
x <- structure(1L, class="myclass")
print(x)
is called from RStudio it produces
printing from rstudio_print
function.
Otherwise it would produce the default print method for the object. However it appears that this approach must be implemented at a level above the global environment as just defining this but evaluating
x
still returns the results from the base:print
.
[1] 1
attr(,"class")
[1] "myclass"