Hello community, I want to export my Leaflet map with custom size in a pdf. Im facing an error in my code. Can anyone help fix it ?
...
library(shiny)
library(leaflet)
library(leaflet.extras)
library(webshot)
ui <- fluidPage(
leafletOutput("map"),
downloadButton("download_pdf", "Download PDF")
)
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(-122.4194, 37.7749, zoom = 10) #set initial view
})
output$download_pdf <- downloadHandler(
filename = "map.pdf",
content = function(file) {
# Create a temporary HTML file with the map
tmp <- tempfile(fileext = ".html")
on.exit(unlink(tmp))
htmlwidgets::saveWidget(output$map, tmp, selfcontained = TRUE)
# Convert the HTML file to PDF using rmarkdown and webshot
rmarkdown::render(tmp, output_format = "pdf_document",
output_file = file,
params = list(width = "21cm", height = "29.7cm"))
}
)
}
shinyApp(ui, server)