R - 传单 - highcharter 工具提示

R - leaflet - highcharter tooltip

我想在我的 leaflet 弹出窗口中包含一个 highcharter 情节。在 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. (example from answer 的帮助下)正是我想要的。我只是不知道如何在 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

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' 功能但没有成功。

一、解决方法:

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"

reprex package (v0.3.0)

于 2019-12-05 创建

我们可以看到它 return 是一个 leaflet js 依赖项列表(截断为前三个)。如果我们对 highcharter 进行相同的尝试,它不会 return 任何依赖(除了强制性的 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

reprex package (v0.3.0)

于 2019-12-05 创建

这是因为highcharter是R包名,不是js库名。你 可以看看list.files(system.file('htmlwidgets', package = 'highcharter'))来 看到库名为 highchart,因此在此使用正确的名称 位:

{...} %>%
  add_deps("highchart", 'highcharter') %>%
  {...}

会成功的 ;)

你好@Pierre 和@MalditoBarbudo,我试图在 shinyApp 中改编你的例子,但我无法运行 正确

请问有什么帮助吗?

library(shiny)
library(tidyverse)
library(htmlwidgets)
library(htmltools)
library(leaflet)
library(highcharter)

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)
  )
}


ui = fluidPage(
  leafletOutput("map")
)

#server.r
server = function(input, output, session) {


  output$map <- renderLeaflet({
    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()

    })
}

shinyApp(ui = ui, server = server)