Renders a ggplot2 object to a temporary PNG file and returns the result as a
Base64-encoded data URI string (data:image/png;base64,...). This
format can be assigned directly to an <img> tag src attribute
in the JavaScript frontend via app$send().
If ggplot2 is not installed, the function stops with a helpful message.
If rendering fails, a fallback error plot is returned instead of NULL.
Value
A single-element character string containing the data URI,
or the output of rdesk_error_plot() if rendering fails.
See also
rdesk_error_plot() for the fallback error plot.
Examples
if (requireNamespace("ggplot2", quietly = TRUE)) {
p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) +
ggplot2::geom_point()
b64 <- rdesk_plot_to_base64(p)
stopifnot(grepl("^data:image/png;base64,", b64))
}
