I have a Shiny application that renders a leaflet map. On this map I use addCircleMarkers to add two different groups containing location markers (one group is called "locations" and another group is called "customers"). I am using observeEvent to allow the user to click on a "locations" marker and then have all "customers" markers become visible that are associated with that particular "location". Clicking on different location markers will show different customers.
I was able to get all this working, and in my code, I have a condition that checks whether a marker from the "locations" group has been clicked, or whether a marker from the "customers" group has been clicked (customers are cleared and redrawn whenever a new location is clicked). However, after incorporating the clusterOptions argument in addCircleMarkers, clicking on a marker would crash the app. After adding a print to console on marker click, I noticed that the "group" argument only appears when clusterOptions is not listed as an argument.
Below is a simplified version of my code that renders a leaflet with with 3 map points. When you click on a marker, the print() output in the R console outputs the group "locations" from the clicked marker.
library(leaflet)
library(dplyr)
library(shiny)
# Create 3 map points -- 2 will be clustered
map_points <- bind_rows(c(location = 'A', lon = -122.4, lat = 37.8),
c(location = 'B', lon = -122.4, lat = 37.8),
c(location = 'C', lon = -118.2, lat = 34.0))
map_points$lon <- as.numeric(map_points$lon)
map_points$lat <- as.numeric(map_points$lat)
# Shiny
ui <- fluidPage(
leafletOutput("mymap")
)
server <- function(input, output, session){
output$mymap <- renderLeaflet({
leaflet(map_points) %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
addCircleMarkers(lng = ~lon,
lat = ~lat,
group = "locations",
layerId = ~location
)
})
observeEvent(input$mymap_marker_click, {
print(input$mymap_marker_click)
}
)
}
shinyApp(ui = ui, server = server)
However, if I take the same code and add a clusterOptions argument to addCircleMarkers, the group disappears:
library(leaflet)
library(dplyr)
library(shiny)
# Create 3 map points -- 2 will be clustered
map_points <- bind_rows(c(location = 'A', lon = -122.4, lat = 37.8),
c(location = 'B', lon = -122.4, lat = 37.8),
c(location = 'C', lon = -118.2, lat = 34.0))
map_points$lon <- as.numeric(map_points$lon)
map_points$lat <- as.numeric(map_points$lat)
# Shiny
ui <- fluidPage(
leafletOutput("mymap")
)
server <- function(input, output, session){
output$mymap <- renderLeaflet({
leaflet(map_points) %>%
addProviderTiles("OpenStreetMap.Mapnik") %>%
addCircleMarkers(lng = ~lon,
lat = ~lat,
group = "locations",
layerId = ~location,
# adding clusterOptions removes the group in observeEvent
clusterOptions = markerClusterOptions()
)
})
observeEvent(input$mymap_marker_click, {
print(input$mymap_marker_click)
}
)
}
shinyApp(ui = ui, server = server)
I'm a new user so I can only post one screenshot, but the print to console shows that $group has disappeared ($clusterId is added from the new argument).
Is there a way I can use the clusterOptions argument without having the group disappear?