ObserveEvent removes addCircleMarkers group when clusterOptions argument is present

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)

with_group

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?

Hi,

I'm not very familiar with Leaflet, but would it help to replace the group argument by clusterId. I think they have same effect and clusterId overwrites group I guess so that's why that one is present now.

addCircleMarkers(lng = ~lon, 
                       lat = ~lat, 
                       clusterId = "locations", 
                       layerId = ~location,
                       # adding clusterOptions removes the group in observeEvent
                       clusterOptions = markerClusterOptions() 
      )

Listening on http://127.0.0.1:7287
$`id`
[1] "C"

$.nonce
[1] 0.9921159

$lat
[1] 34

$lng
[1] -118.2

$clusterId
[1] "locations"

Hope this helps,
PJ

Thanks for the reply. I'm not sure clusterId is actually supposed to replace the group argument though.
Elsewhere in my code (not in the simplified example) I use a clearGroup(group = "customers") function to remove all markers on the map that belong to the "customers" group, but nested within an IF statement that only runs when a marker from the "locations" group is clicked. The purpose is to only show customers that are associated with a location that a user selects from the map (clicking on a different customer will not clear and re-draw customers).

Using clusterId in place of group isn't ideal for me because Leaflet doesn't seem to have a built-in function equivalent to clearGroup that takes in the clusterId as the parameter and then clears all markers belonging to that clusterId. I'm thinking that it might be a bug that clusterId overwrites group, since you can specify both arguments in the function.

Looking at the Leaflet documentation, the closest function I can see where you can specify one clusterId to remove is "removeMarkerFromCluster(map, layerId, clusterId)" but this removes markers within clusters (based on the layerId of the marker), and not the entire cluster itself.

https://www.rdocumentation.org/packages/leaflet/versions/2.0.2/topics/removeControl

Maybe I can write out code that iterates through the markers and removes them, but my map has thousands of markers and I feel like this would really slow down the visualization compared to running something equivalent to clearGroup.

Hi,

I'm not sure if this issue is a bug or there is a reason why group is not displayed. In case no one else can help here, I suggest you open a ticket on the package's gitHub and ask the creators.

PJ

I went ahead and asked my question on the Leaflet package's gitHub. Judging by the large backlog of open issues, I'm not sure when or if I'll get a response. Hoping I hear back!

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