Search code examples
rleafletpng

Backdrop map tiles missing in R Leaflet .png output


I used leaflet.extras::addBingMaps() to pull in some Bing Maps tiles to visualise an area in China, and then tried to save the map to a .png file using mapview::mapshot() to include in my report:

library(leaflet)
library(leaflet.extras)
library(mapview)

leaflet() %>% 
  fitBounds(lng1 = 117.3, lat1 = 40.9, lng2 = 117.4, lat2 = 41.0) %>% 
  addBingTiles(
    apikey = "REPLACE_WITH_YOUR_API_KEY",
    imagerySet = "CanvasLight"
  ) %>%
  addScaleBar(position = "bottomleft") %>% 
  mapshot(
    file = "test.png",
    remove_controls = c("zoomControl", "layersControl", "homeButton")
  )

In the saved .png file I found missing tiles:

enter image description here

However, if not output as the .png file, the interactive leaflet map rendered correctly.

I wonder whether it was because addBingTiles() timed out, but did not find in its documentation how to extend the waiting time to render all tiles. As I am writing this question, I found some other people having similar problems but not in R. Therefore, I would really appreciate any experienced R user's help.


Solution

  • Thanks to @TimSalabim, who brought my attention to that mapview::mapshot() also accepts the delay argument passed to webshot::webshot(). This would allow extra waiting time to render all the map elements before the screenshot is taken – problem solved!