title: Shiny mapshot to export leaflet into a knitted document
body: <p>I have a shiny app I'm using to visualize a variety of data. One of the plots produced is a map. I'm allowing the user to download all the plots at once as a single Word doc using a knitted document. I would like to include the map in the document, but can't figure out how to do that. I can export a png (or pdf) of the map itself with a separate <code>downloadHandler</code>, but ideally want the map included in the main doc. Any help would be high appreciated... If anyone has tips for the extent of the downloaded map (which does not seem to match the extent of the map on the screen), that would also be awesome.</p>
  <p>I'm open to using <code>officer</code> to import the downloaded map into the newly created doc file, but can't figure out how to a) do it with a single <code>downloadHandler</code>, and b) tell R how to handle the name of the latest download of the map.</p>
  <pre><code># reproducible example of the shiny app, mimicking the functionality and structure of the full app. 
  library(shiny)
  library(dplyr)
  library(leaflet)
  library(mapview)
  library(ggplot2)
  
  df &lt;- structure(list(Lon = c(-111.584650079555, -112.17670350598, -111.585725614472, -112.173232931394, -111.772792415394), Lat = c(41.7797872701221, 43.0098749960118, 41.7489995541869, 43.0096673539034, 42.1053681392244), Size = c(1:5)), row.names = c(NA, -5L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  
  server = function(input, output){
      # baseline map
      mymap &lt;- reactive({
        leaflet(df) %&gt;%
          setView(lng = -111.6, lat = 41.8, zoom = 8) %&gt;%
          addProviderTiles(&quot;Esri.WorldImagery&quot;, layerId = &quot;basetile&quot;,
              options = providerTileOptions(minZoom = 8, opacity = 0.75)) })
  
      # to be able to use leafletproxy
      output$map &lt;- renderLeaflet({
        mymap() })
  
      # quick plot to show how I'm exporting my actual plots
      plot.calc &lt;- reactive({
        p &lt;- ggplot(df) + geom_point(aes(x = Lon, y = Lat))
        return(p) })  
      
      output$plot &lt;- renderPlot({
        plot.calc() })
  
      # helper function to use with leafleproxy, to allow for export of the user-created map
      myfun &lt;- function(map, df.in, bounds){
          bounds &lt;- InBounds()$bounds
          latRng &lt;- range(bounds$north, bounds$south)
          lngRng &lt;- range(bounds$east, bounds$west)
              
        addCircleMarkers(map, data = df.in, lng = df.in$Lon, lat = df.in$Lat, radius = ~Size * 4, color = &quot;red&quot;)  %&gt;%
        fitBounds(min(lngRng), min(latRng), max(lngRng), max(latRng)) 
                                              }
      
      # pull out data within the zoomed-in boundarier of the map  
      InBounds &lt;- reactive({
          req(input$map_bounds)
              
          bounds &lt;- input$map_bounds
          latRng &lt;- range(bounds$north, bounds$south)
          lngRng &lt;- range(bounds$east, bounds$west)
                                      
          df.in &lt;- df %&gt;%
                  filter(Lat &gt;= latRng[1], Lat &lt;= latRng[2],
                          Lon &gt;= lngRng[1], Lon &lt;= lngRng[2])
          output &lt;- list(df.in = df.in, bounds = bounds) 
                              }) 
      # update map with the data within the map boundarier                        
      observe({
        leafletProxy(&quot;map&quot;) %&gt;% myfun(InBounds()$df.in)
      })
      
      # map that will be downloaded
      mapdown &lt;- reactive({
        bounds &lt;- input$map_bounds
        latRng &lt;- range(bounds$north, bounds$south)
        lngRng &lt;- range(bounds$east, bounds$west)
        mymap() %&gt;% myfun(InBounds()$df.in) 
      })
  
      # handler for downloading all plots (but not maps)
      output$plot_down &lt;- downloadHandler(
          filename = 'Plots.docx',
  
        content = function(file) {
          src &lt;- normalizePath(c('Plots.Rmd', 'template_word2.docx')) # SEE HERE
          owd &lt;- setwd(tempdir())
          on.exit(setwd(owd))
          file.copy(src, c('Plots.Rmd', 'template_word2.docx'), overwrite = TRUE) # SEE HERE
          params &lt;- list(Plot = plot.calc())
          
          Sys.setenv(RSTUDIO_PANDOC=&quot;C:/Program Files/RStudio/bin/pandoc&quot;)
          out &lt;- rmarkdown::render('Plots.Rmd', output_file = file, params = params, envir = new.env(parent = globalenv()))
          file.rename(out, file)
                          })
      
      # handler showing that I can download a png of the map itself                   
      output$map_down &lt;- downloadHandler(
        filename = 'mymap.png',
  
        content = function(file) {
          owd &lt;- setwd(tempdir())
          on.exit(setwd(owd))
          mapshot(mapdown(), file = file, cliprect = &quot;viewport&quot;)
                          })}
  
  ui &lt;- fluidPage(
       sidebarPanel(downloadButton('map_down', &quot;Download map&quot;),
                      downloadButton('plot_down', &quot;Download plots&quot;)), 
       mainPanel(leafletOutput(&quot;map&quot;),
                  plotOutput(&quot;plot&quot;)))
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  <p>Rmd file:</p>
  <pre><code>---
  title: &quot;Title&quot;
  output: 
    word_document:
      reference_docx: template_word2.docx
    
  params:
    Plot: NA
  ---
  
  ```{r, echo = FALSE, warning = FALSE, fig.width = 6.4, fig.height = 3.5}
  params$Plot
  ```
    
  Plot exports ok
  </code></pre>