All examples By author By category About

timelyportfolio

R leaflet tooltips with listviewer jsonedit

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