一、解决办法:
leaflet() %>%
addTiles() %>%
addCircleMarkers(lat = 45.4, lng = 14.9,
popup = list(paste(as.character(
hchart(data.frame(x = 1:10, y = 1:10), type = "line", hcaes(x = x, y = y)) %>% hc_size(width = 300, height = 200)
))),
popupOptions = popupOptions(minWidth = 300, maxHeight = 200)) %>%
onRender(
"
function(el,x) {
this.on('popupopen', function() {HTMLWidgets.staticRender();})
}
") %>%
add_deps("highchart", 'highcharter') %>%
browsable()
而现在,为什么呢?
这是由于add_deps
功能:
add_deps <- function(dtbl, name, pkg = name) {
tagList(
dtbl,
htmlwidgets::getDependency(name, pkg)
)
}
正如你所看到的,它在内部使用htmlwidgets::getDependency
。如果我们尝试用leaflet
包裹:
library(htmlwidgets)
getDependency('leaflet')[1:3]
#> [[1]]
#> List of 10
#> $ name : chr "htmlwidgets"
#> $ version : chr "1.5.1"
#> $ src :List of 1
#> ..$ file: chr "/home/malditobarbudo/R/x86_64-pc-linux-gnu-library/3.6/htmlwidgets/www"
#> $ meta : NULL
#> $ script : chr "htmlwidgets.js"
#> $ stylesheet: NULL
#> $ head : NULL
#> $ attachment: NULL
#> $ package : NULL
#> $ all_files : logi TRUE
#> - attr(*, "class")= chr "html_dependency"
#>
#> [[2]]
#> List of 10
#> $ name : chr "jquery"
#> $ version : chr "1.12.4"
#> $ src :List of 1
#> ..$ file: chr "/home/malditobarbudo/R/x86_64-pc-linux-gnu-library/3.6/leaflet/htmlwidgets/lib/jquery"
#> $ meta : NULL
#> $ script : chr "jquery.min.js"
#> $ stylesheet: NULL
#> $ head : NULL
#> $ attachment: NULL
#> $ package : NULL
#> $ all_files : logi TRUE
#> - attr(*, "class")= chr "html_dependency"
#>
#> [[3]]
#> List of 10
#> $ name : chr "leaflet"
#> $ version : chr "1.3.1"
#> $ src :List of 1
#> ..$ file: chr "/home/malditobarbudo/R/x86_64-pc-linux-gnu-library/3.6/leaflet/htmlwidgets/lib/leaflet"
#> $ meta : NULL
#> $ script : chr "leaflet.js"
#> $ stylesheet: chr "leaflet.css"
#> $ head : NULL
#> $ attachment: NULL
#> $ package : NULL
#> $ all_files : logi TRUE
#> - attr(*, "class")= chr "html_dependency"
Created on 2019-12-05 by the reprex package (v0.3.0)
我们可以看到它返回一个列表leaflet
js 依赖项(截断为前三个)。如果我们尝试同样的highcharter
它不返回任何依赖项(除了强制的 htmlwidgets 依赖项)
library(htmlwidgets)
getDependency('highcharter')
#> [[1]]
#> List of 10
#> $ name : chr "htmlwidgets"
#> $ version : chr "1.5.1"
#> $ src :List of 1
#> ..$ file: chr "/home/malditobarbudo/R/x86_64-pc-linux-gnu-library/3.6/htmlwidgets/www"
#> $ meta : NULL
#> $ script : chr "htmlwidgets.js"
#> $ stylesheet: NULL
#> $ head : NULL
#> $ attachment: NULL
#> $ package : NULL
#> $ all_files : logi TRUE
#> - attr(*, "class")= chr "html_dependency"
#>
#> [[2]]
#> NULL
Created on 2019-12-05 by the reprex package (v0.3.0)
这是因为highcharter
是 R 包名称,而不是 js 库名称。你
可以看看list.files(system.file('htmlwidgets', package = 'highcharter'))
到
看到这个库被称为高图,因此在此使用正确的名称
少量:
{...} %>%
add_deps("highchart", 'highcharter') %>%
{...}
会成功的;)