R - 传单 - highcharter 工具提示 [英] R - leaflet - highcharter tooltip
问题描述
我想在我的 leaflet
弹出窗口中包含一个 highcharter
图.在
不幸的是,它不仅仅是将 add_deps
更改为 highcharter
leaflet() %>%addTiles() %>%addCircleMarkers(lat = 45.4, lng = 14.9,弹出=列表(粘贴(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)) %>%渲染("函数(el,x){this.on('popupopen', function() {HTMLWidgets.staticRender();})}") %>%add_deps("highcharter") %>%可浏览()
我尝试修改了popupopen"功能,但没有成功.
一、解决方案:
leaflet() %>%addTiles() %>%addCircleMarkers(lat = 45.4, lng = 14.9,弹出=列表(粘贴(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)) %>%渲染("函数(el,x){this.on('popupopen', function() {HTMLWidgets.staticRender();})}") %>%add_deps("highchart", 'highcharter') %>%可浏览()
现在,为什么?
这是由于 add_deps
函数:
add_deps <- function(dtbl, name, pkg = name) {标记列表(dtbl,htmlwidgets::getDependency(name, pkg))}
如您所见,它在内部使用 htmlwidgets::getDependency
.如果我们尝试使用 leaflet
包:
库(htmlwidgets)getDependency('传单')[1:3]#>[[1]]#>10 人名单#>$ 名称 : chr "htmlwidgets"#>$版本:chr1.5.1"#>$ src :列表 1#>..$ 文件:chr/home/malditobarbudo/R/x86_64-pc-linux-gnu-library/3.6/htmlwidgets/www"#>$元:NULL#>$脚本:chrhtmlwidgets.js"#>$ 样式表:NULL#>$头:空#>$附件:NULL#>$包:空#>$ all_files : 逻辑真#>- attr(*, "class")= chr "html_dependency"#>#>[[2]]#>10 人名单#>$ 名称 : chr "jquery"#>$版本:chr1.12.4"#>$ src :列表 1#>..$ 文件:chr/home/malditobarbudo/R/x86_64-pc-linux-gnu-library/3.6/leaflet/htmlwidgets/lib/jquery"#>$元:NULL#>$脚本:chrjquery.min.js"#>$ 样式表:NULL#>$头:空#>$附件:NULL#>$包:空#>$ all_files : 逻辑真#>- attr(*, "class")= chr "html_dependency"#>#>[[3]]#>10 人名单#>$名称:chr传单"#>$版本:chr1.3.1"#>$ src :列表 1#>..$ 文件:chr/home/malditobarbudo/R/x86_64-pc-linux-gnu-library/3.6/leaflet/htmlwidgets/lib/leaflet"#>$元:NULL#>$脚本:chrleaflet.js"#>$ 样式表:chr "leaflet.css"#>$头:空#>$附件:NULL#>$包:空#>$ all_files : 逻辑真#>- attr(*, "class")= chr "html_dependency"
由 reprex 包 (v0.3.0) 于 2019 年 12 月 5 日创建支持>
我们可以看到它返回了一个 leaflet
js 依赖项列表(截断为前三个).如果我们对 highcharter
尝试相同的操作,它不会返回任何依赖项(除了强制性的 htmlwidgets 依赖项)
库(htmlwidgets)getDependency('highcharter')#>[[1]]#>10 人名单#>$ 名称 : chr "htmlwidgets"#>$版本:chr1.5.1"#>$ src :列表 1#>..$ 文件:chr/home/malditobarbudo/R/x86_64-pc-linux-gnu-library/3.6/htmlwidgets/www"#>$元:NULL#>$脚本:chrhtmlwidgets.js"#>$ 样式表:NULL#>$头:空#>$附件:NULL#>$包:空#>$ all_files : 逻辑真#>- attr(*, "class")= chr "html_dependency"#>#>[[2]]#>空值
由 reprex 包 (v0.3.0) 于 2019 年 12 月 5 日创建支持>
这是因为 highcharter
是 R 包名,而不是 js 库名.你可以看看 list.files(system.file('htmlwidgets', package = 'highcharter'))
到看到该库被称为 highchart,所以在此使用正确的名称位:
{...} %>%add_deps("highchart", 'highcharter') %>%{...}
会成功的;)
I want to include a highcharter
plot in my leaflet
popup. With help from this post Iam able to include a sparkline
plot. However, due to my lack of html skills I dont know how to modify the code to work with highcharter
. This answer on SO (example from answer) is exactly what I want. I just dont know how to implement in in R
.
library(leaflet)
library(tidyverse)
library(htmlwidgets)
library(htmltools)
library(sparkline)
library(highcharter)
# Step 1 convert htmlwidget to character representation of HTML components
as.character.htmlwidget <- function(x, ...) {
htmltools::HTML(
htmltools:::as.character.shiny.tag.list(
htmlwidgets:::as.tags.htmlwidget(
x
),
...
)
)
}
add_deps <- function(dtbl, name, pkg = name) {
tagList(
dtbl,
htmlwidgets::getDependency(name, pkg)
)
}
This works fine:
leaflet() %>%
addTiles() %>%
addCircleMarkers(lat = 45.4, lng = 14.9,
popup = list(paste(as.character(sparkline(1:19))))) %>%
onRender(
"
function(el,x) {
this.on('popupopen', function() {HTMLWidgets.staticRender();})
}
") %>%
add_deps("sparkline") %>%
browsable()
Unfortunately its not just change add_deps
to highcharter
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("highcharter") %>%
browsable()
I have tried modified the 'popupopen' function without success.
First, the solution:
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()
And now, why?
This is due to the add_deps
function:
add_deps <- function(dtbl, name, pkg = name) {
tagList(
dtbl,
htmlwidgets::getDependency(name, pkg)
)
}
As you can see, it uses internally htmlwidgets::getDependency
. If we try with leaflet
package:
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)
we can see that it returns a list of leaflet
js dependencies (truncated to the first three). If we try the same for highcharter
it does not return any dependency (besides the mandatory htmlwidgets dependency)
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)
This is because highcharter
is the R package name, not the js library name. You
can look at list.files(system.file('htmlwidgets', package = 'highcharter'))
to
see that the library is called highchart, so using the correct name in this
bit:
{...} %>%
add_deps("highchart", 'highcharter') %>%
{...}
will do the trick ;)
这篇关于R - 传单 - highcharter 工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!