This question is a follow-up to the questions How to save a leaflet map in Shiny, and Save leaflet map in Shiny.
I add a toolbar to draw shapes/points on the map that is addDrawToolbar
in the leaflet.extras package. That lets users to draw lines, shapes, ... interactively. In the end I want one to be able to save the map with the drawn shapes as a pdf or png.
I have coded up the following making use of the answer to the question: How to save a leaflet map in Shiny. But it does not help achieve my goal.
Is there anyone who can help me?
library(shiny)
library(leaflet)
library(leaflet.extras)
library(mapview)
ui <- fluidPage(
leafletOutput("map"),
br(),
downloadButton("download_pdf", "Download .pdf")
)
server <- function(input, output, session) {
foundational_map <- reactive({
leaflet() %>%
addTiles()%>%
addMeasure(
primaryLengthUnit = "kilometers",
secondaryAreaUnit = FALSE
)%>%
addDrawToolbar(
targetGroup='draw',
editOptions = editToolbarOptions(selectedPathOptions =
selectedPathOptions()),
polylineOptions = filterNULL(list(shapeOptions =
drawShapeOptions(lineJoin = "round",
weight = 3))),
circleOptions = filterNULL(list(shapeOptions =
drawShapeOptions(),
repeatMode = F,
showRadius = T,
metric = T,
feet = F,
nautic = F))) %>%
setView(lat = 45, lng = 9, zoom = 3) %>%
addStyleEditor(position = "bottomleft",
openOnLeafletDraw = TRUE)
})
output$map <- renderLeaflet({
foundational_map()
})
user_created_map <- reactive({
foundational_map() %>%
setView(lng = input$map_center$lng, lat = input$map_center$lat,
zoom = input$map_zoom)
})
output$download_pdf <- downloadHandler(
filename = paste0("map_", Sys.time(), ".pdf"),
content = function(file) {
mapshot(user_created_map(), file = file)
}
)
}
shinyApp(ui = ui, server = server)