首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何跳过在htmlwidgets::saveWidget()中编写依赖项?

如何跳过在htmlwidgets::saveWidget()中编写依赖项?
EN

Stack Overflow用户
提问于 2018-07-06 23:20:42
回答 1查看 939关注 0票数 2

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

代码语言:javascript
复制
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

这仅在依赖中产生约6MB的数据(串音-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写入依赖关系,尽管这些文件已经存在。这是可以防止的吗?

EN

回答 1

Stack Overflow用户

发布于 2018-07-11 02:12:47

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

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51213463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档