我想用R和plot.ly制作一些交互式图形.当我在R-Studio中运行以下代码时,它会生成一个交互式图形.
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以生成多个网页.
将plot_ly
对象分配给变量,然后使用htmlwidgets::saveWidget()
保存实际文件,如下所示:
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")