Exporting PNG files from Plotly in R without internet

旧巷老猫 提交于 2019-12-01 01:11:47

问题


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.


回答1:


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:


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.



来源:https://stackoverflow.com/questions/33998996/exporting-png-files-from-plotly-in-r-without-internet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!