Created
March 9, 2018 21:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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