title: Save leaflet map in shiny with user selected layers
body: <p>I have a shiny app in which I have a leaflet map. I have managed to include the ability for an individual to save the map with their own preferred zoom and boundaries (thanks to <a href="https://stackoverflow.com/a/47988859/7500692">this</a> answer) and can remove the zoom controls in the saved image (thanks to <a href="https://stackoverflow.com/a/48669670/7500692">this</a> answer). However, I would now like to be able to save the map with the layers that the user has selected themselves.</p>
  
  <p>Here is a short example code which runs:</p>
  
  <pre><code>library(shiny)
  library(tidyverse)
  library(leaflet)
  library(mapview)
  
  ui &lt;- fluidPage(
    fluidPage(
      leafletOutput(outputId = "map"),
      downloadButton(outputId = "save")
    )
  )
  
  server &lt;- function(input, output, session) {
  
    map &lt;- reactive({
      leaflet() %&gt;%
        setView(lng = -117, lat = 37, zoom = 7) %&gt;%
        addTiles() %&gt;%
        addMarkers(lng = -115.172813, lat = 36.114647,
                   group = "Vegas") %&gt;%
        addMarkers(lng = -119.538330, lat = 37.865101,
                   group = "Yosemite") %&gt;%
        addLayersControl(overlayGroups = c("Vegas", "Yosemite"),
                         options = layersControlOptions(collapsed = F)) %&gt;%
        hideGroup("Yosemite")
    })
  
    output$map &lt;- renderLeaflet({
      map()
    })
  
    output$save &lt;- downloadHandler(
      filename = "map.png",
      content = function(file){
        latRng &lt;- range(input$map_bounds$north,
                        input$map_bounds$south)
        lngRng &lt;- range(input$map_bounds$east,
                        input$map_bounds$west)
        m &lt;- map() %&gt;%
          setView(lng = (lngRng[1] + lngRng[2])/2,
                  lat = (latRng[1] + latRng[1])/2,
                  zoom = input$map_zoom)
        m$x$options &lt;- append(m$x$options, list("zoomControl" = F))
        mapshot(m, file = file)
      }
    )
  
  }
  
  shinyApp(ui, server)
  </code></pre>
  
  <p>In this app the image that is created when a user clicks Download only includes the default layer Yosemite, even if Vegas has been selected as well/instead.</p>
  
  <p>I am also interested in a way of hiding the layerControl options in the saved image but this is secondary to my main question.</p>
  
  <p>Thanks</p>