Search code examples
rshinyr-leaflet

Shiny leaflet - points not plotted when layerId is set


I have a Shiny app with a leaflet map. I need to be able to set a layerId value on the map for my full app. However, when I do that, most of my points disappear. If I remove the layerId, all my points are displayed correctly. Minimum example below - comment out the layerId line to see the issue I'm having.

library(shiny)
library(leaflet)

df <- structure(list(Lon = c(-111.279, -111.388, -111.441, -111.899, 
-111.189, -111.182, -111.151, -111.146, -111.131, -111.109, -111.474
), Lat = c(43.864, 44.055, 44.176, 44.727, 43.809, 43.807, 43.805, 
43.811, 43.812, 43.816, 44.259)), row.names = c(NA, -11L), class = c("tbl_df", 
"tbl", "data.frame"))


leaflet(df) %>%
            addTiles() %>%
            addCircleMarkers(lng = ~Lon, lat = ~Lat,
            layerId = "1") ### <----- problem child here
    

Solution

  • df %>% 
      mutate(id = row_number()) %>% 
      leaflet() %>%  
      addTiles() %>%
      addCircleMarkers(lng = ~Lon, lat = ~Lat, layerId = ~id)