| export {plotly} | R Documentation |
Export a plotly graph to a static file
export(p = last_plot(), file = "plotly.png", selenium = NULL, ...)
p |
a plotly or ggplot object. |
file |
a filename. The file type is inferred from the file extension. Valid extensions include 'jpeg' | 'png' | 'webp' | 'svg' | 'pdf' |
selenium |
used only when |
... |
if |
For SVG plots, a screenshot is taken via webshot::webshot().
Since phantomjs (and hence webshot) does not support WebGL,
the RSelenium package is used for exporting WebGL plots.
Carson Sievert
# The webshot package handles non-WebGL conversion to jpeg/png/pdf
## Not run:
export(plot_ly(economics, x = ~date, y = ~pce))
export(plot_ly(economics, x = ~date, y = ~pce), "plot.pdf")
# svg/webp output or WebGL conversion can be done via RSelenium
if (requireNamespace("RSelenium")) {
rD <- RSelenium::rsDriver(browser = "chrome")
export(
plot_ly(economics, x = ~date, y = ~pce), "plot.svg", rD
)
export(
plot_ly(economics, x = ~date, y = ~pce, z = ~pop), "yay.svg", rD
)
}
# If you can't get a selenium server running, another option is to
# use Plotly.downloadImage() via htmlwidgets::onRender()...
# Downloading images won't work inside RStudio, but you can set the viewer
# option to NULL to prompt your default web browser
options(viewer = NULL)
plot_ly(economics, x = ~date, y = ~pce, z = ~pop) %>%
htmlwidgets::onRender(
"function(el, x) {
var gd = document.getElementById(el.id);
Plotly.downloadImage(gd, {format: 'png', width: 600, height: 400, filename: 'plot'});
}"
)
## End(Not run)