Save leaflet map in Shiny
<p>I have created a leaflet map in a Shiny application. Now I need a download button, so that the user can download the currently shown map including all markers, polygons etc. as a pdf file. </p>

<p>I have found this solution how to save a leaflet map in R: <a href="https://stackoverflow.com/questions/31336898/how-to-save-leaflet-in-rstudio-map-as-png-or-jpg-file">How to save Leaflet in R map as png or jpg file?</a></p>

<p>But how does it work in Shiny? I kept the example code simple, but think of it, as if there were a lot of changes to the map via leafletProxy() before the user wants to save the map as a pdf.</p>

<p>This is my try, but it's not working.</p>

<p>server.R</p>

<pre><code>library(shiny)
library(leaflet)
library(devtools)
install_github("wch/webshot") # first install phantomjs.exe in your directory

library(htmlwidgets)
library(webshot)

server &lt;- function(input, output){

  output$map &lt;- renderLeaflet({
    leaflet() %&gt;% addTiles()
  })

 observe({
    if(input$returnpdf == TRUE){
      m &lt;- leafletProxy("map")
      saveWidget(m, "temp.html", selfcontained = FALSE)
      webshot("temp.html", file = "plot.pdf", cliprect = "viewport")
    }
  })

  output$pdflink &lt;- downloadHandler(
    filename &lt;- "map.pdf",
    content &lt;- function(file) {
      file.copy("plot.pdf", file)
    }
  )
}
</code></pre>

<p>ui.R</p>

<pre><code>ui &lt;- fluidPage(
     sidebarPanel(
     checkboxInput('returnpdf', 'output pdf?', FALSE), 
     conditionalPanel(
       condition = "input.returnpdf == true",
       downloadLink('pdflink')
      ) 
     ), 
     mainPanel(leafletOutput("map"))
)
</code></pre>