Following on from this question, I am looking to save and download a leaflet map as a png or jpeg image. I have the following code but I keep getting an error.
ui <- fluidPage(
leafletOutput("map"),
downloadButton("dl")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles()
})
output$dl <- downloadHandler(
filename = "map.png",
content = function(file) {
mapshot(input[["map"]], file = file)
}
)
}
shinyApp(ui = ui, server = server)
The error I get when I try to download (by clicking the button) is
Warning: Error in system.file: 'package' must be of length 1
Stack trace (innermost first):
65: system.file
64: readLines
63: paste
62: yaml.load
61: yaml::yaml.load_file
60: getDependency
59: widget_dependencies
58: htmltools::attachDependencies
57: toHTML
56: <Anonymous>
55: do.call
54: mapshot
53: download$func [#11]
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
Error : 'package' must be of length 1
Bonus points if you can tell me how to get this working with leafletProxy
.
May be this would help:
Overview
Since 'leaflet' maps are interactive, the leaflet object being used in
mapview::mapshot()
function must be interactive. Accounting for this allows for the user to save their version of a leaflet map within the Shiny app.Final result
Once you run the Shiny app, open the app in a new window.
Once in the browser, go ahead and click
Download
. It took about ~3 seconds.Once
Download
has been clicked, you'll promptly see a PDF file wherever your downloaded files are stored on your machine.References
My ideas sprung from the following posts:
Save leaflet map in Shiny
How to save a leaflet map in Shiny
Input/Events - Leaflet for R
Thanks to @blondeclover, there is no need to store the
bounds
of the leaflet map when usingsetView()
. Instead, simply useinput$MAPID_center$lng
andinput$MAPID_center$lat
when usingsetView()
.