The Leaflet package includes functions to show and hide map layers. You can allow users to decide what layers to show and hide, or programmatically control the visibility of layers using server-side code in Shiny.
In both cases, the fundamental unit of showing/hiding is the group.
A group is a label given to a set of layers. You assign layers to groups by using the
group parameter when adding the layers to the map.
leaflet() %>% addTiles() %>% addMarkers(data = coffee_shops, group = "Food & Drink") %>% addMarkers(data = restaurants, group = "Food & Drink") %>% addMarkers(data = restrooms, group = "Restrooms")
Many layers can belong to same group. But each layer can only belong to zero or one groups (you can’t assign a layer to two groups).
Groups vs. Layer IDs
Groups and Layer IDs may appear similar, in that both are used to assign a name to a layer. However, they differ in that layer IDs are used to provide a unique identifier to individual markers and shapes, etc., while groups are used to give shared labels to many items.
You generally provide one
group value for the entire
addMarkers call, and you can reuse that same
group value in future
addXXX calls to add to that group’s membership (as in the example above).
layerId arguments are always vectorized: when calling e.g.
addMarkers you need to provide one layer ID per marker, and they must all be unique. If you add a circle with a layer ID of
"foo" and later add a different shape with the same layer ID, the original circle will be removed.
Interactive Layer Display
You can use Leaflet’s layers control feature to allow users to toggle the visibility of groups.
outline <- quakes[chull(quakes$long, quakes$lat),] map <- leaflet(quakes) %>% # Base groups addTiles(group = "OSM (default)") %>% addProviderTiles("Stamen.Toner", group = "Toner") %>% addProviderTiles("Stamen.TonerLite", group = "Toner Lite") %>% # Overlay groups addCircles(~long, ~lat, ~10^mag/5, stroke = F, group = "Quakes") %>% addPolygons(data = outline, lng = ~long, lat = ~lat, fill = F, weight = 2, color = "#FFFFCC", group = "Outline") %>% # Layers control addLayersControl( baseGroups = c("OSM (default)", "Toner", "Toner Lite"), overlayGroups = c("Quakes", "Outline"), options = layersControlOptions(collapsed = FALSE) ) map
addLayersControl function distinguishes between base groups, which can only be viewed one group at a time, and overlay groups, which can be individually checked or unchecked.
Although base groups are generally tile layers, and overlay groups are usually markers and shapes, there is no restriction on what types of layers can be placed in each category.
Only one layers control can be present on a map at a time. If you call
addLayersControl multiple times, the last call will win.
Programmatic Layer Display
You can use the
hideGroup functions to show and hide groups from code. This mostly makes sense in a Shiny context with
leafletProxy, where perhaps you might toggle group visibility based on input controls in a sidebar.
You can also use
hideGroup in conjuction with
addLayersControl to set which groups are checked by default.
map %>% hideGroup("Outline")
Finally, you can remove the layers in a group using
clearGroup. Note that this doesn’t just remove the layers from the group, it also removes them from the map. (It does not, however, remove the group itself from the map; it still exists, but is empty.)