how to increase the resolution of leaflet maps in R Shiny

1.1k Views Asked by At

I am struggling how to save leaflet maps in a Shiny app in a high resolution.

In the following code a leaflet map is made, and the map can be downloaded by pressing the 'download' button:

library(shiny)
library(htmlwidgets)
library(leaflet)
library(webshot)

ui <- fluidPage(
  leafletOutput("map"),
  downloadButton("download")
)

server <- function(input, output, session) {
  leafletOptions(resolutions = 1200)

  global <- reactiveValues(map = 0)

  output$map <- renderLeaflet({
    global$map <- leaflet() %>% 
                  fitBounds(3.31497114423, 50.803721015, 7.09205325687, 53.5104033474) %>%
                  addTiles()
  })

  output$download <- downloadHandler(filename = "map.png", content = function(file)
  {
    global$map$x$options <- append(global$map$x$options, 
                                   list("zoomControl" = FALSE))

    saveWidget(global$map, 
               "temp.html", 
               selfcontained = FALSE)

    webshot("temp.html", 
            file = file, 
            zoom = 1, 
            vwidth = 800, 
            vheight = 555, 
            cliprect = "viewport")
  })   
}

shinyApp(ui = ui, server = server)

With this code I would like to download an image file that contains the map in a better quality, i.e. in a higher resolution, especially when zooming in in the image.

I experimented with the 'zoom' option in webshot, but this only makes the image larger, the quality is not any better. I do not see any resolution parameter in the function 'leafletOutput'. I experimented with 'leafletOptions(resolutions = ...)' but it did not help. In the function 'saveWidget' I added 'knitrOptions = list(dpi=1200)' as parameter of the fuction 'saveWidget', but without success.

Any ideas how to improve the quality of leaflet maps downloaded in Shiny?

0

There are 0 best solutions below