0

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?

WJH
  • 539
  • 5
  • 14
  • I haven't used `webshot` before but after reading the documentation, you could try increasing the viewport size. Make it large. – rbasa Dec 05 '21 at 12:47
  • Thanks for the suggestion. I have tried this. The problem, however, is that when making the map large by making the viewport large, the labels of place names become relatively (much) smaller. – WJH Dec 10 '21 at 12:34
  • That's what the tileserver gives you. You can't change tileserver images from R or leaflet. You have to modify the tileserver options. You'll likely need to run your own tileserver to do what you want. – rbasa Jan 04 '22 at 05:27
  • Something with providerTileOptions? – WJH Jan 06 '22 at 21:42
  • 1
    You will have to build and run your own openstreetmap server, and modify the stylesheet to your liking. Or, subscribe to a service like Mapbox. They allow font size changes if I am not mistaken. – rbasa Jan 10 '22 at 15:17
  • OK, like here https://www.linuxbabe.com/ubuntu/openstreetmap-tile-server-ubuntu-18-04-osm ? – WJH Jan 11 '22 at 16:10
  • 1
    Yes. The settings you are looking for to change the font size of the labels should be in the stylesheet. For questions about that, the openstreetmap forum would be a better place than here (R). – rbasa Jan 13 '22 at 11:17

0 Answers0