evaluate_and_highlight {downlit} | R Documentation |
Evaluate code and syntax highlight the results
Description
This function runs code
and captures the output using
evaluate::evaluate()
. It syntax higlights code with highlight()
, and
intermingles it with output.
Usage
evaluate_and_highlight(
code,
fig_save,
classes = downlit::classes_pandoc(),
env = NULL,
output_handler = evaluate::new_output_handler(),
highlight = TRUE
)
Arguments
code |
Code to evaluate (as a string). |
fig_save |
A function with arguments |
classes |
A mapping between token names and CSS class names.
Bundled |
env |
Environment in which to evaluate code; if not supplied, defaults to a child of the global environment. |
output_handler |
Custom output handler for |
highlight |
Optionally suppress highlighting. This is useful for tests. |
Value
An string containing HTML with a dependencies
attribute
giving an additional htmltools dependencies required to render the HTML.
Examples
cat(evaluate_and_highlight("1 + 2"))
cat(evaluate_and_highlight("x <- 1:10\nmean(x)"))
# -----------------------------------------------------------------
# evaluate_and_highlight() powers pkgdown's documentation formatting so
# here I include a few examples to make sure everything looks good
# -----------------------------------------------------------------
blue <- function(x) paste0("\033[34m", x, "\033[39m")
f <- function(x) {
cat("This is some output. My favourite colour is ", blue("blue"), ".\n", sep = "")
message("This is a message. My favourite fruit is ", blue("blueberries"))
warning("Now at stage ", blue("blue"), "!")
}
f()
plot(1:10)