使用R和plot.ly-如何编写脚本将输出保存为网页 [英] Using R and plot.ly - how do I script saving my output as a webpage
问题描述
我想使用R和 plot.ly 制作一些交互式图形.当我在R-Studio中运行以下代码时,它会生成一个交互式图形.
I want to make some interactive graphs using R and plot.ly. When I run the following code in R-Studio, it produces an interactive graph.
library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
mode = "markers", color = carat, size = carat)
生成此图后,当我单击R-Studio的图"窗口中的导出"按钮时,它为我提供了将图另存为网页的选项.如何编写将生成的图另存为网页的过程的脚本?我的最终目标是从bash脚本内部迭代运行Rscripts以生成多个网页.
After producing this graph, when I click on the "Export" button in the Plot window of R-Studio, it gives me the option to save the plot as a webpage. How can I script the process of saving produced plots as webpages? My ultimate goal is to run Rscripts iteratively from inside a bash script to produce multiple webpages.
推荐答案
将plot_ly
对象分配给变量,然后使用htmlwidgets::saveWidget()
保存实际文件,如下所示:
Assign the plot_ly
object to a variable and then use htmlwidgets::saveWidget()
to save the actual file, like so:
library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- plot_ly(d, x = carat, y = price, text = paste("Clarity: ", clarity),
mode = "markers", color = carat, size = carat)
htmlwidgets::saveWidget(as_widget(p), "index.html")
这篇关于使用R和plot.ly-如何编写脚本将输出保存为网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!