Skip to content

Instantly share code, notes, and snippets.

@anadiedrichs
Created March 9, 2018 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anadiedrichs/68dbadd4135acc9a51ff8520a5cd987c to your computer and use it in GitHub Desktop.
Save anadiedrichs/68dbadd4135acc9a51ff8520a5cd987c to your computer and use it in GitHub Desktop.
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