2

我开始在 R 中使用 plot.ly,我对使用 htmlwidgets 直接在 html 中发布我的图表的可能性感到惊讶。到目前为止,我无法在同一个 html 中保存多个小部件。我在独立的 html 中保存了多个小部件,而不是在 html 代码中手动组合它,但我希望能够在 R 中完成。

一个简单的例子:

#graph
graph<- ggplot(df, aes(x = Data, y=tax))+ geom_bar(stat='identity')
gg <- ggplotly(graph)

# save as HtmlWigdet
htmlwidgets::saveWidget(as.widget(gg), "Index.html")

如何解析多个 ggplotly 对象以保存小部件?

(这是我在stackoverflow中的第一个问题,希望我做对了!问候!)

4

2 回答 2

3

这是我从包的点点滴滴改编的功能,htmltools用于保存标签列表,然后返回iframe标签。您可以用 包装多个htmlwidgetshtmltools::tagList然后使用此功能保存整个一堆。

save_tags <- function (tags, file, selfcontained = F, libdir = "./lib") 
{
  if (is.null(libdir)) {
    libdir <- paste(tools::file_path_sans_ext(basename(file)), 
                    "_files", sep = "")
  }
  htmltools::save_html(tags, file = file, libdir = libdir)
  if (selfcontained) {
    if (!htmlwidgets:::pandoc_available()) {
      stop("Saving a widget with selfcontained = TRUE requires pandoc. For details see:\n", 
           "https://github.com/rstudio/rmarkdown/blob/master/PANDOC.md")
    }
    htmlwidgets:::pandoc_self_contained_html(file, file)
    unlink(libdir, recursive = TRUE)
  }
  return(htmltools::tags$iframe(src= file, height = "400px", width = "100%", style="border:0;"))
}
于 2016-11-21T03:13:19.000 回答
1

你追求的用例是什么?您可能需要考虑将这些图表添加到Flexdashboard(在 R Markdown 中创建)。这是我最近的 goto,与 Plotly 结合使用。

于 2016-11-11T21:04:21.360 回答