block by timelyportfolio 098a1bf77caf2fd0267e3116f128ad13

R leaflet tooltips with plotly htmlwidget

Full Screen

forked from timelyportfolio‘s block: R leaflet tooltips 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)
library(plotly)

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) {
        plot_ly(x=~1:20, y=~runif(20)) %>%
          add_markers() %>%
          as.tags() %>%
          {tags$div(style="width:300px;", .)} %>%
          as.character()
      }
    )
  ) %>%
  onRender(
"
function(el,x) {
  this.on('popupopen', function() {HTMLWidgets.staticRender();})
}
") %>%
  add_deps("plotly") %>%
  htmltools::attachDependencies(plotly:::plotlyMainBundle(), append = TRUE) %>%
  htmltools::attachDependencies(crosstalk::crosstalkLibs(), append = TRUE) %>%
  browsable()