使用R和plot.ly - 我怎么脚本救了我的输出为网页

问题描述 投票:18回答:2

我想要使​​用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。

r plotly
2个回答
43
投票

分配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")

0
投票

更新安德鲁answer为R-3.5.1和plotly-4.8.0,即:

library(plotly)
set.seed(100)
d <- diamonds[sample(nrow(diamonds), 1000), ]
p <- plot_ly(d, x = ~carat, y = ~price, text=~paste("Clarity : ", clarity))
htmlwidgets::saveWidget(as_widget(p), "index.html")

为了得到这个work,你还需要安装pandoc。在CentOS / RedHat的做yum install pandoc pandoc-citeproc。在Mac OSX,使用homebrew,做brew install pandoc

该解决方案进行了测试和OSX 10.13.6工程工作在R-3.5.1。

© www.soinside.com 2019 - 2024. All rights reserved.