Show DT inside a leaflet tooltip

Hi!

I am trying to build an app that uses leaflet to plot some data over a map. The data consists in nodes and edges that connect them
I would like to show something like a DT table inside the tooltip of each marker, showing the data of the marker (i.e. name, lat, long, edge_id) in the form of an interactive datatable.

Does anyone have an idea how to do this?

Thanks in advance!

I am not certain about DT tables, but plain ol' HTML table is a legit part of a Leaflet popup.

By the way this is one use case where {leaflet} used directly beats othervise highly recommended {tmap} package. tmap sanitizes HTML and can not be used to build popups like these.

For an example in action consider this code, built on good old NC shapefile:

library(sf)
library(leaflet)

shape <- st_read(system.file("shape/nc.shp", package="sf")) %>%  # included with sf package
  st_transform(4326)

shape$label <- paste0("<table style=\"border: 1px solid black\">
                        <caption>this is a county!</caption>
                        <tr>
                          <th>county</th>
                          <th>fips code</th>
                        <tr>
                        <tr>
                          <td>", shape$NAME, "</td>
                          <td style = \"text-align: right\">", shape$FIPSNO, "</td>
                        <tr>
                      </table>")

leaflet(shape) %>% 
  addProviderTiles("Stamen.Toner") %>% 
  addPolygons(color = "blue",
              fillColor = "aliceblue",
              popup = ~label)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.