html - 使用R和plot.ly,如何将多个htmlwidgets保存到我的html中?

标签 html r ggplot2 plotly htmlwidgets

我开始使用 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 对象来 saveWidgets?

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

最佳答案

这是我根据 htmltools 包的一些片段改编的函数,用于保存标记列表,然后返回 iframe 标记。您可以使用 htmltools::tagList 包装多个 htmlwidgets,然后使用此函数保存整个集合。

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;"))
}

关于html - 使用R和plot.ly,如何将多个htmlwidgets保存到我的html中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540802/

相关文章:

javascript - Jquery 选择器在我的函数调用中返回随机“

html - div 的定位仅在 Firefox 中发生变化

jQuery 切换下拉菜单不起作用

r - 如何将未加引号的列名列表提供给 `lapply`(以便我可以将它与 `dplyr` 函数一起使用)

javascript - 从 plotly 的任何地方 plotly 地点击事件

R ggplot2 为组均值绘制条形图

java - 如何在虚拟机/远程计算机上的工作区中启动本地 html 文件

r - xgboost 质量是如何计算的?

r - 将不同数据集中的 geom_text 添加到 geom_bar

r - 使用ggplot2仅绘制时间