Search code examples
rleafletflextable

Insert FlexTable in Leaflet PopUp


Is there a way to insert Flextables in leaflet Popups? I have tried several ways, including converting the Flextable to HTML code. I would like to do it directly from the Flextable code without transformation to htmlTables package or similar. Here is a sample code:

lat <- c(43.22693)
lon <- c(-86.20437)
map <- leaflet(options = leafletOptions(minZoom = 2, maxZoom = 18)) %>%
  addTiles()%>%
  setView(lng = -97, 
          lat = 39, zoom = 4) %>%
  setMaxBounds(lng1 = -180, 
               lat1 = -60, 
               lng2 = 180, 
               lat2 = 65) %>% 
  addCircleMarkers(lat = lat, lng = lon, popup = flextable(mtcars)) 
map

Solution

  • Something like this?

    library(leaflet)
    library(flextable)
    
    lat <- c(43.22693)
    lon <- c(-86.20437)
    map <- leaflet(options = leafletOptions(minZoom = 2, maxZoom = 18)) %>%
      addTiles()%>%
      setView(lng = -97, 
              lat = 39, zoom = 4) %>%
      setMaxBounds(lng1 = -180, 
                   lat1 = -60, 
                   lng2 = 180, 
                   lat2 = 65) %>% 
      # addCircleMarkers(lat = lat, lng = lon, popup = to_html(flextable(mtcars), type = "html")) #default
      addCircleMarkers(lat = lat, lng = lon, popup = to_html(flextable(mtcars), type = "img")) # as an image
    map