7

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.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
Murta
  • 2,037
  • 3
  • 25
  • 33

2 Answers2

5

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")
JaKu
  • 1,096
  • 16
  • 29
3

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.

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • I have downloaded phantomjs. But when I am running the above script I get the following error: `Error in find_phantom() : PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.` Do you have any idea how to fix this? – Jonathan Rhein May 30 '16 at 09:06
  • 1
    you have dowloaded it, but did you install it? – MLavoie May 30 '16 at 09:09
  • I doubleclicked on the `phantomjs` file in `bin`directory and terminal opened reading: `Last login: Mon May 30 10:47:54 on ttys001 /Users/Jonathan/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs ; exit; JonathasMacBook:~ Jonathan$ /Users/Jonathan/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs ; exit; phantomjs> ` – Jonathan Rhein May 30 '16 at 09:55
  • I was on a Mac and I don't remember having issues – MLavoie May 30 '16 at 10:51
  • I don't know if it matters, but I do have macports on my laptop. phantoms was stored under macports. – MLavoie May 30 '16 at 14:02