7
votes

In this question, Exporting PNG files from Plotly in R I asked how to export Plotly plots to disk.

I used the function plotly_IMAGE, but later discovered that the function uses the Plotly internet servers.

The question is, now that Plotly JavaScript is local, how can I create a png local file without internet?

I tried this code, without success:

library(plotly)
png(filename = "test.png")
plot_ly(x = 1:10)
dev.off()

The idea is to make it programaticaly, without click on export button over the chart.

2

2 Answers

4
votes

They've added a new export function to the plotly package. But to my knowledge it does the same thing as @MLavoie's answer suggests. Usage:

p <- plot_ly(...)
export(p, file = "test.png")
2
votes

You will to need install Phantom (http://phantomjs.org/download.html) which is quite easy and you can try this:

library(plotly)
library(webshot)
library(htmlwidgets)

m <- plot_ly(x = 1:10)
saveWidget(as.widget(m), "temp.html")
webshot("temp.html", file = "test.png",
        cliprect = "viewport")

you will find temp.html and temp.png in your working directory.