title: How to update the leaflet map in the selectModUI in a Shiny app?
body: <p>I would like to update the <code>selectModUI</code> from the <code>mapedit</code> package for different <code>leaflet</code> maps when using <code>Shiny</code>. Below is a working example.</p>
  
  <pre><code>library(tidyverse)
  library(shiny)
  library(sf)
  library(leaflet)
  library(mapview)
  library(mapedit)
  library(DT)
  library(viridis)
  
  # Load the sf object
  nc &lt;- st_read(system.file("shape/nc.shp", package = "sf"))
  
  # Project transformation
  nc &lt;- st_transform(nc, crs = 4326)
  
  # Create a color function for the leaflet map
  sid74_pal &lt;- colorBin(palette = viridis(10), domain = nc$SID74, bins = 4)
  
  # Create a leaflet map
  sid74_map &lt;- leaflet() %&gt;%
    addTiles(group = "OSM") %&gt;%
    addProviderTiles("CartoDB", group = "CartoDB") %&gt;%
    addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %&gt;%
    addFeatures(nc, 
                color = ~sid74_pal(SID74), 
                label = ~htmltools::htmlEscape(NAME),
                layerId = ~seq_len(length(st_geometry(nc)))) %&gt;%
    addLegend(position = "bottomright", pal = sid74_pal, 
              values = nc$SID74,
              title = "SID74") %&gt;%
    addLayersControl(baseGroups = c("OSM", "CartoDB", "Esri.WorldImagery"))
  
  ui &lt;- fluidPage(
    # Select Module Output
    h3("Map"),
    selectModUI(id = "Sel_Map"),
    # Datatable Output
    h3("Table"),
    dataTableOutput(outputId = "Table")
  )
  
  server &lt;- function(input, output) {
    # Create selectMod
    sel &lt;- callModule(selectMod, "Sel_Map", sid74_map)
  
    # Reactive values
    rv &lt;- reactiveValues(
      selectnum = NULL,
      sub_table = nc %&gt;% 
        st_set_geometry(NULL) %&gt;%
        slice(0)
    )
  
    # Subset the table based on the selection
    observe({
      # the select module returns a reactive
      gs &lt;- sel()
      # Filter for the county data
      rv$selectnum &lt;- as.numeric(gs[which(gs$selected == TRUE), "id"])
  
      if (!is.null(rv$selectnum)){
        rv$sub_table &lt;- nc %&gt;% 
          st_set_geometry(NULL) %&gt;%
          slice(rv$selectnum) 
      }
    })
  
    # Create a datatable
    output$Table &lt;- renderDataTable({
      datatable(rv$sub_table, options = list(scrollX = TRUE))
    })
  
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>The idea is to create a map and users can select or unselect the polygons on the map. Based on the users' selection, a data table output would dynamically show which counties are selected and present the data, as the screenshot shows.</p>
  
  <p><a href="https://i.stack.imgur.com/gTbBR.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gTbBR.png" alt="enter image description here"></a></p>
  
  <p>Now I want to add a select input so users can decide which parameter they want to visualize using the app. I feel like I can create some kinds of reactivities or reactive values to store the maps, and then update the  Below is an example I created. Notice that compared to Example 1, I created a new leaflet map called <code>sid79_map</code> in Example 2 and add a select input so people can select. However, this strategy is not working. It would be great if someone can point out a direction to go.</p>
  
  <pre><code>library(tidyverse)
  library(shiny)
  library(sf)
  library(leaflet)
  library(mapview)
  library(mapedit)
  library(DT)
  library(viridis)
  
  # Load the sf object
  nc &lt;- st_read(system.file("shape/nc.shp", package = "sf"))
  
  # Project transformation
  nc &lt;- st_transform(nc, crs = 4326)
  
  # Create a color function for the leaflet map
  sid74_pal &lt;- colorBin(palette = viridis(10), domain = nc$SID74, bins = 4)
  sid79_pal &lt;- colorBin(palette = viridis(10), domain = nc$SID79, bins = 4)
  
  # Create a leaflet map
  sid74_map &lt;- leaflet() %&gt;%
    addTiles(group = "OSM") %&gt;%
    addProviderTiles("CartoDB", group = "CartoDB") %&gt;%
    addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %&gt;%
    addFeatures(nc, 
                color = ~sid74_pal(SID74), 
                label = ~htmltools::htmlEscape(NAME),
                layerId = ~seq_len(length(st_geometry(nc)))) %&gt;%
    addLegend(position = "bottomright", pal = sid74_pal, 
              values = nc$SID74,
              title = "SID74") %&gt;%
    addLayersControl(baseGroups = c("OSM", "CartoDB", "Esri.WorldImagery"))
  
  sid79_map &lt;- leaflet() %&gt;%
    addTiles(group = "OSM") %&gt;%
    addProviderTiles("CartoDB", group = "CartoDB") %&gt;%
    addProviderTiles("Esri.WorldImagery", group = "Esri.WorldImagery") %&gt;%
    addFeatures(nc, 
                color = ~sid79_pal(SID79), 
                label = ~htmltools::htmlEscape(NAME),
                layerId = ~seq_len(length(st_geometry(nc)))) %&gt;%
    addLegend(position = "bottomright", pal = sid79_pal, 
              values = nc$SID79,
              title = "SID79") %&gt;%
    addLayersControl(baseGroups = c("OSM", "CartoDB", "Esri.WorldImagery"))
  
  ui &lt;- fluidPage(
    # Select input
    selectInput(inputId = "Selection", label = "Select Counties", choices = c("SID74", "SID79"), selected = "SID74"),
    # Select Module Output
    h3("Map"),
    selectModUI(id = "Sel_Map"),
    # Datatable Output
    h3("Table"),
    dataTableOutput(outputId = "Table")
  )
  
  server &lt;- function(input, output) {
  
    # Try to create reactivity based on the select input type, not working
    sel_type &lt;- reactive({
      input$Selection
    })
  
    leafmap &lt;- reactive({
      if(sel_type() == "SID74"){
        sid74_map
      } else if (sel_type() == "SID79"){
        sid79_map
      }
    })
  
    # Create selectMod
    sel &lt;- callModule(selectMod, "Sel_Map", leafmap())
  
    # Reactive values
    rv &lt;- reactiveValues(
      selectnum = NULL,
      sub_table = nc %&gt;% 
        st_set_geometry(NULL) %&gt;%
        slice(0)
    )
  
    # Subset the table based on the selection
    observe({
      # the select module returns a reactive
      gs &lt;- sel()
      # Filter for the county data
      rv$selectnum &lt;- as.numeric(gs[which(gs$selected == TRUE), "id"])
  
      if (!is.null(rv$selectnum)){
        rv$sub_table &lt;- nc %&gt;% 
          st_set_geometry(NULL) %&gt;%
          slice(rv$selectnum) 
      }
    })
  
    # Create a datatable
    output$Table &lt;- renderDataTable({
      datatable(rv$sub_table, options = list(scrollX = TRUE))
    })
  
  }
  
  # Run the application 
  shinyApp(ui = ui, server = server)
  </code></pre>