r语言 - 如何在 htmlwidgets::saveWidget() 中跳过编写依赖项?



当使用plotly可视化数据时,我想将小部件编写为html文档,而无需每次都htmlwidgets::saveWidget编写依赖项,假设这些依赖项已经到位,以节省处理时间。小部件需要自包含以节省磁盘空间。

library(plotly)
t <- Sys.time()
p <- plot_ly(ggplot2::diamonds, y = ~price, color = ~cut, type = "box")
htmlwidgets::saveWidget(as_widget(p), "test.html", selfcontained = F, libdir = NULL)
print(Sys.time() - t)

Time difference of 4.303076 secs在我的机器上。

这仅在 depedencies 中产生 ~6 MB 的数据(crosstalk-1.0.0, htmlwidgets-1.2, jquery-1.11.3, plotly-binding-4.7.1.9000, plotly-htmlwidgets-css-1.38.3, plotly-main-1.38.3, typedarray-0.1(

htmlwidgets::saveWidget写入依赖项,尽管这些文件已经存在。这能预防吗?

好问题。 我试图在代码中的注释中内联回答。htmlwidgets依赖项来自两个来源:htmlwidgets::getDependency()和小组件列表中的dependencies元素。 将dependencies中的src元素更改为href而不是file意味着不会复制这些依赖项。 但是,来自htmlwidgets::getDependency()的依赖项更难覆盖,但在这种情况下只会复制htmlwidgets.jsplotly-binding.js,与其他四个相比,它们相当小。

library(plotly)
p <- plot_ly(ggplot2::diamonds, y = ~price, color = ~cut, type = "box")
# let's inspect our p htmlwidget list for clues
p$dependencies
# if the src argument for htmltools::htmlDependency
#   is file then the file will be copied
#   but if it is href then the file will not be copied
# start by making a copy of your htmlwidget
#   this is not necessary but we'll do to demonstrate the difference
p2 <- p
p2$dependencies <- lapply(
p$dependencies,
function(dep) {
# I use "" below but guessing that is not really the location
dep$src$href = "" # directory of your already saved dependency
dep$src$file = NULL

return(dep)
}
)
# note this will still copy htmlwidgets and plotly-binding
#  requires a much bigger hack to htmlwidgets::getDependency() to change
t <- Sys.time()
htmlwidgets::saveWidget(as_widget(p), "test.html", selfcontained = F, libdir = NULL)
print(Sys.time() - t)
t <- Sys.time()
htmlwidgets::saveWidget(as_widget(p2), "test.html", selfcontained = F, libdir = NULL)
print(Sys.time() - t)

相关内容

  • 没有找到相关文章

最新更新