block by timelyportfolio 33db1fb9e64257ef7149754bdff0b2e0

R leaflet tooltips with sparkline htmlwidget

Full Screen

forked from timelyportfolio‘s block: R leaflet popups with sparkline htmlwidget


See DT issue

helper functions

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

make tooltips

library(leaflet) # needs leaflet > 1 timelyportfolio/leaflet@v1.0
library(htmlwidgets)
library(htmltools)

df <- read.csv(textConnection(
  "Name,Lat,Long
  Samurai Noodle,47.597131,-122.327298
  Kukai Ramen,47.6154,-122.327157
  Tsukushinbo,47.59987,-122.326726"
))

leaflet(df) %>% addTiles() %>%
  addMarkers(
    ~Long, ~Lat,
    popup = lapply(seq_along(df),function(x)as.character(sparkline(runif(20))))
  ) %>%
  onRender(
"
function(el,x) {
  this.on('popupopen', function() {HTMLWidgets.staticRender();})
}
") %>%
  add_deps("sparkline") %>%
  browsable()