Как пропустить запись зависимостей в 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 на моей машине.

Это создает ~ 6 МБ данных только в зависимостях (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 пишет зависимости, хотя эти файлы уже существуют. Можно ли это предотвратить?


person Comfort Eagle    schedule 06.07.2018    source источник


Ответы (1)


Хороший вопрос. Я попытался ответить в комментариях в коде. htmlwidgets зависимости исходят из двух источников: htmlwidgets::getDependency() и элемента dependencies в списке виджетов. Изменение элемента src в dependencies на href вместо file означает, что эти зависимости не будут скопированы. Однако зависимости от htmlwidgets::getDependency() сложнее перезаписать, но в этом случае будут скопированы только htmlwidgets.js и plotly-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)
person timelyportfolio    schedule 10.07.2018