Learn R Programming

mapgl (version 0.3.1)

clear_legend: Clear legends from a map

Description

Remove one or more legends from a Mapbox GL or MapLibre GL map in a Shiny application.

Usage

clear_legend(map, legend_ids = NULL)

Value

The updated map proxy object with the specified legend(s) cleared.

Arguments

map

A map proxy object created by mapboxgl_proxy() or maplibre_proxy().

legend_ids

Optional. A character vector of legend IDs to clear. If not provided, all legends will be cleared.

Examples

Run this code
if (FALSE) {
# In a Shiny server function:

# Clear all legends
observeEvent(input$clear_all, {
  mapboxgl_proxy("map") %>%
    clear_legend()
})

# Clear specific legends by ID
observeEvent(input$clear_specific, {
  mapboxgl_proxy("map") %>%
    clear_legend(legend_ids = c("legend-1", "legend-2"))
})

# Clear legend after removing a layer
observeEvent(input$remove_layer, {
  mapboxgl_proxy("map") %>%
    remove_layer("my_layer") %>%
    clear_legend(legend_ids = "my_layer_legend")
})
}

Run the code above in your browser using DataLab