Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Save Locally Generated Plot Locally As PNG/JPG + save png image from plotly or ggplot object
library(plotly)
library(webshot)
#uncomment the following to run the first time for installing
# webshot::install_phantomjs()
library(htmltools)
# source https://github.com/ropensci/plotly/issues/311
export <- function(p, file = "plotly.png", ...) {
if (system.file(package = "webshot") == "") {
stop(
'Please install the webshot package ',
'(if not on CRAN, try devtools::install_github("wch/webshot"))' )
}
f <- basename(tempfile('plotly', '.', '.html'))
on.exit(unlink(f), add = TRUE)
html <- htmlwidgets::saveWidget(plotly_build(p), f)
webshot::webshot(f, file, ...)
}
data(cars)
just_a_plot <- plot_ly(data=cars, x=~speed, y=~dist, type="scatter")
export(just_a_plot,file = "/home/ana/test-plot.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.