我开始使用 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/