Open egnha opened 6 years ago
This is an issue in the repr package, which circumvents dispatching print
to the proper method when the object class is "function"
. As a workaround, one can explicitly invoke print()
.
Implement and export repr::repr_html
methods for Gestalt's function classes.
Initial implementation:
# repr/R/utils.r:
html_escape <- local({
html_specials <- c(
`&` = "&",
`<` = "<",
`>` = ">"
)
pre_wrap <- "<span style=white-space:pre-wrap>%s</span>"
function(text) {
for (chr in names(html_specials))
text <- gsub(chr, html_specials[[chr]], text, fixed = TRUE)
consec_spaces <- grepl("\ \ ", text)
text[consec_spaces] <- sprintf(pre_wrap, text[consec_spaces])
text
}
})
#' @importFrom utils capture.output
as_repr_html <- function(class) {
printer <- get(paste0("print.", class))
wrap <- "<pre class=language-r><code>%s</code></pre>"
function(obj, ...) {
code <- capture.output(printer(obj))
sprintf(wrap, paste(html_escape(code), collapse = "\n"))
}
}
#' @importFrom repr repr_html
NULL
#' @export
repr_html.CompositeFunction <- as_repr_html("CompositeFunction")
#' @export
repr_html.PartialFunction <- as_repr_html("PartialFunction")
Enable option to syntax-highlight.
Executing a cell like
ignores the class and prints
whereas
applies the correct print method
Similar problem occurs for other custom gestalt classes (
"PartialFunction"
, etc.).Is this an issue with Jupyter and/or gestalt?