问题描述
我想在leaflet
弹出窗口中包含一个highcharter
图.在此帖子的帮助下,我可以包含sparkline
图.但是,由于我缺乏html技能,因此我不知道如何修改代码以与highcharter
一起使用. 关于SO的答案(答案示例)正是我想要的.我只是不知道如何在R
中实现.
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)
)
}
这很好:
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()
不幸的是,它不仅将add_deps
更改为highcharter
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()
我尝试修改'popupopen'函数,但没有成功.
I have tried modified the 'popupopen' function without success.
推荐答案
首先,解决方案:
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
软件包:
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"
我们可以看到它返回了leaflet
js依赖项列表(被截断为前三个).如果我们对highcharter
尝试相同的操作,它将不返回任何依赖关系(除强制性的htmlwidgets依赖关系之外)
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
这是因为highcharter
是R软件包名称,而不是js库名称.你可以看一下list.files(system.file('htmlwidgets', package = 'highcharter'))
看到该库被称为 highchart ,因此在此使用正确的名称位:
This is because highcharter
is the R package name, not the js library name. Youcan look at list.files(system.file('htmlwidgets', package = 'highcharter'))
tosee that the library is called highchart, so using the correct name in thisbit:
{...} %>%
add_deps("highchart", 'highcharter') %>%
{...}
可以解决问题;)
这篇关于R-传单-Highcharter工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!