1
votes

I have a r leaflet map that has multiple layers and each layer can be checked or unchecked. I am trying to find a way to have a select all/deselect all option.

Below is my code where the overlays are reactive groups. df() is a reactive dataframe myLocation() is a reactive location (long,lat)

So in the map in the upper right hand corner is where I want a select all/ deselect all option

groups <- reactive({as.character(unique(df()$Folder))})
groupColors <- reactive({
    colorFactor(palette = rainbow(length(groups())), 
                domain = df()$Folder)
})

output$mymap <- renderLeaflet({
    leaflet() %>%
    addTiles() %>% 
    leaflet::addMarkers(lng=c(myLocation()[1]), lat=c(myLocation()[2]), 
         popup  = paste("Lat/Long: ",myLocation()[2],"/",myLocation()[1]),
         popupOptions = popupOptions(maxWidth = 1000, closeOnClick = TRUE)) 
    %>%

############more code here      
    #this section is where a select all/ deselect option has to be placed
    addLayersControl(overlayGroups=groups(),options = 
        layersControlOptions(collapsed = TRUE)) 
})
1
Please provide us with a full reproducible exampleismirsehregal
I forgot to include the libraries leaflet and geosphere. I can't really provide everything since df comes from a csv full of lat/longs and the Folder column is the name of each location. The more code part just is adding more markers which are not relevant. The myLocation is just a given c(long,lat)MLS

1 Answers

0
votes

Following shiny tutorial... try using leafletProxy to modify your existing map and use the hideGroup() function from leaflet to add/remove the entire group.