Update an existing leaflet map in a context of multiple map sets and corresponding shapefiles.

update_mapset(trigger, shp, mapset, ids, selected_regions, all_regions,
  all_locs, all_locs_shapefile = all_locs, default = "", click_id, session,
  map = "Map", color = "#3C8DBC", fill = color, color_opacity = 1,
  fill_opacity = 0.4)

Arguments

trigger

character, "selectInput" or "mapclick". Must used both in an app. See details.

shp

a shapefile pertaining to a single mapset. See build_mapset.

mapset

character, name of a mapset in mapsets. See build_mapset.

ids

literal names of regions in shp (may not match regions elements).

selected_regions

a subset of all_regions.

all_regions

a named vector of region IDs pertaining to regions in shp.

all_locs

a named list (names match mapsets) where each list element is a vector of regions.

all_locs_shapefile

a named list (names match mapsets) where each list element is a vector of regions specified to match the region IDs in shp.

default

name of a default map set, if used. Otherwise defaults to default="".

click_id

character, the leaflet map click id.

session

the app session variable.

map

character, name of leaflet map object.

color

border color for selected polygons.

fill

fill color for selected polygons.

color_opacity

numeric, border color opacity.

fill_opacity

numeric, fill color opacity.

Value

side effects only, called from within Shiny observers to update selectInput and leaflet output map.

Details

This function updates a leaflet map made using build_mapset. It is called from within Shiny observers. See examples. The function is used twice in two contexts in an app in conjunction with a single original build_mapset call for a single leaflet output instance. Specifically, one call is within an observer that reacts to map polygon mouse clicks and the other call is within an observer that reacts to changes to the shiny::selectInput that is synced to the leaflet map polygon selections.

The function takes a number of the same arguments passed to build_mapset. Compared to build_mapset, color, fill and opacity is simplified to single values each since this function only deals with the adding of selected polygons to the map, not the adding of unselected polygons or polygon mouse hover options.

Note that the selectInput observer context (trigger="selectInput") requires all arguments to be supplied except for click_id and session, which are needed only for trigger="mapclick". In the case of the latter, the only arguments needed (or used) are trigger, click_id, session and selected_regions. All other arguments may be left out. Map clicks are observed strictly to update the synchronized selectInput, not to remove or add polygons directly. In turn, changes to the selectInput, whether by the user directly, or by updating on map click observation, are responsible for updating map polygon layers.

Examples

# NOT RUN {
# Observe polygon selectInput.
# Add or remove selected polygons leaflet map polygons.
observeEvent(input$regions, {
  update_mapset("selectInput", shp, mapset, ids,
    input$regions, all_regions, all_locs)
}, ignoreNULL=FALSE)

# Observe leaflet map shape click.
# Update region selectInput.
observeEvent(input$Map_shape_click, {
  update_mapset("mapclick", selected_regions=input$regions,
                click_id=input$Map_shape_click$id, session=session)
})
# }