block by timelyportfolio 5cc94e2f3aca678bd993109675aa292d

R leaflet tooltips with listviewer jsonedit

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)
library(listviewer)

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(i){
        as.character(
          jsonedit(
            jsonlite::toJSON(df[i,,drop=TRUE],auto_unbox=TRUE),
            height=200,width=200
          )
        )
      }
    )
  ) %>%
  onRender(
"
function(el,x) {
  this.on('popupopen', function() {HTMLWidgets.staticRender();})
}
") %>%
  add_deps("jsonedit", "listviewer") %>%
  browsable()