rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
805 stars 509 forks source link

toggle baseGroups legends in the same way as overlayGroups in addLayersControl #895

Open mkoohafkan opened 9 months ago

mkoohafkan commented 9 months ago

Currently, legends associated with layers in overlayGroups will toggle on/off via the layer selection, but legends associated with layers in baseGroups are always shown, regardless of which basegroup layer is selected. Not sure if this is a bug.

Reprex:

library(sf)
library(spData)
library(leaflet)

layers = split(us_states, us_states$REGION)
region_pal = colorFactor("Dark2", names(layers))

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

for (region in names(layers)) {
    mp <- mp %>%
      addPolygons(data = layers[[region]], group = region,
        color = region_pal(region)) |>
      addLegend(pal = region_pal, values = region)
}

mp %>%
  addLayersControl(baseGroups = names(layers))

image

Expected behavior: Only the legend of the selected layer is shown.

Actual behavior: all legends for all layers are shown, regardless of selection.

Workaround: use overlayGroups instead of baseGroups in addLayersControl(), although this is suboptimal for cases where only one layer in the group should be selectable.

See also https://github.com/rstudio/leaflet/issues/654

monteirojaf commented 2 months ago

Any news on that?