问题描述
我想使用 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 - 如何编写脚本将输出保存为网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!