4

I am trying to accomplish the equivalent of Ghybs Leaflet example found here, where selecting/deselecting an overlay group shows/hides the markers for a group and updates the clustering accordingly using R's leaflet package.

There is a partial solution with R here:

quakes <- quakes %>%
  dplyr::mutate(mag.level = cut(mag,c(3,4,5,6),
                            labels = c('>3 & <=4', '>4 & <=5', '>5 & <=6')))

quakes.df <- split(quakes, quakes$mag.level)

l <- leaflet() %>% addTiles()

names(quakes.df) %>%
  purrr::walk( function(df) {
    l <<- l %>%
      addMarkers(data=quakes.df[[df]],
                      lng=~long, lat=~lat,
                      label=~as.character(mag),
                      popup=~as.character(mag),
                      group = df,
                      clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F),
                      labelOptions = labelOptions(noHide = F,
                                                   direction = 'auto'))
  })

 l %>%
   addLayersControl(
     overlayGroups = names(quakes.df),
     options = layersControlOptions(collapsed = FALSE)
 )

This solution does not cluster markers across groups, even though they are proximate points. A heads up for anyone trying to recreate this solution: you need to remove label=~as.character(mag), and labelOptions = labelOptions(noHide = F, direction = 'auto') in order for the code to run.

How can I accomplish cross-group clustering while maintaining layer control ?

msoderstrom
  • 537
  • 1
  • 7
  • 14

0 Answers0