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.
I have found this solution how to save a leaflet map in R: How to save Leaflet in R map as png or jpg file?
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.
This is my try, but it's not working.
server.R
library(shiny)
library(leaflet)
library(devtools)
install_github("wch/webshot") # first install phantomjs.exe in your directory
library(htmlwidgets)
library(webshot)
server <- function(input, output){
  output$map <- renderLeaflet({
    leaflet() %>% addTiles()
  })
 observe({
    if(input$returnpdf == TRUE){
      m <- leafletProxy("map")
      saveWidget(m, "temp.html", selfcontained = FALSE)
      webshot("temp.html", file = "plot.pdf", cliprect = "viewport")
    }
  })
  output$pdflink <- downloadHandler(
    filename <- "map.pdf",
    content <- function(file) {
      file.copy("plot.pdf", file)
    }
  )
}
ui.R
ui <- fluidPage(
     sidebarPanel(
     checkboxInput('returnpdf', 'output pdf?', FALSE), 
     conditionalPanel(
       condition = "input.returnpdf == true",
       downloadLink('pdflink')
      ) 
     ), 
     mainPanel(leafletOutput("map"))
)