rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
798 stars 505 forks source link

Request: popups with htmlwidgets #376

Open tiernanmartin opened 7 years ago

tiernanmartin commented 7 years ago

I am interested in embedding a DT table in a leaflet popup. I posed this question to the Shiny Google group a couple months ago and haven't heard anything, so I thought I'd try posting an issue here instead.

More specifically, I would really like to add a DT table with a sparkline, something like this (non-nonsensical) photoshop mockup: leaflet-dt-sparkline

My approach would be to make use of two excellent htmlwidget pacakges (DT and sparkline), but it seems that leaflet popups don't display htmlwidgets.

Here's a reprex that illustrates the incompatibility:

library(leaflet)
library(DT)
library(tibble)

# Test data
site <- tribble(
                  ~ Name,      ~Lat,        ~Lon,
        'Samurai Noodle', 47.597131, -122.327298
)

test_dt <- DT::datatable(site)

leaflet(site) %>% 
        addTiles() %>%
  addMarkers(~Lon, ~Lat, 
             popup = test_dt
             )
tim-salabim commented 7 years ago

package mapview has a function that allows for htmlwidget rendering in popups. Internally this uses an iframe to embed the complete widget so this becomes slow very quickly but should be fine for a small number of features:

library(leaflet)
library(DT)
library(tibble)
library(mapview)

# Test data
site <- tribble(
  ~ Name,      ~Lat,        ~Lon,
  'Samurai Noodle', 47.597131, -122.327298
)

test_dt <- DT::datatable(site, width = 500)

leaflet(site) %>% 
  addTiles() %>%
  addMarkers(~Lon, ~Lat, 
             popup = popupGraph(test_dt, type = "html", width = 600),
             popupOptions = popupOptions(maxWidth = 1000)
  )
tiernanmartin commented 7 years ago

Thanks, @tim-salabim!

This solution works, although users will probably want to tweak the default css styling.

I haven't explored the impact of overloading a map with many of these, but I'm sure I will run into this threshold at some point so I'll report back once I do.

While this is a suitable solution, it would be better if htmlwidget-popups were supported by the leaflet package, so I'll leave this issue open for the time being.