6

I am trying to save an R leaflet map, using saveWidget() or outputting an rmd-file to html - as described here: Saving leaflet output as html

When generating an html-file with markdown, the map will show fine in the internal rstudio viewer, however if I open the generated html file or the html-file produced by saveWidget() in a browser, only the circles are shown, not the tiles.

Minimal example:

```{r}
library(leaflet)
library(htmlwidgets)

m <- leaflet(data.frame(lat = 55.71654, lng = 12.47484))
m <-  addCircles(m, popup = "testpopup")
m <-  addTiles(m)
m
saveWidget(m, file="testmap.html")
```
Community
  • 1
  • 1

2 Answers2

6

Add %>% addProviderTiles(providers$OpenStreetMap), that worked for me. names(providers) gives you a list of the layers

pax1a
  • 61
  • 3
  • Yes, this is the answer. – mgriebe Apr 06 '18 at 16:02
  • Any idea why this doesn't work with `providers$CartoDB`? – rrs Jul 19 '18 at 18:10
  • What if it's not one of the provider tiles? I'm using a url to pull the tiles. – ethan tenison Jun 21 '21 at 02:47
  • I just had a similar problem with adding a raster with `leafem:::addGeoRaster`. Worked fine in Rstudio, but not on Firefox/ Safari (not even the auto-pan worked). But on Chrome, everything seems to work. No clue why. However, I had messed around with the zIndex as described here: https://stackoverflow.com/questions/34159578/raster-image-goes-below-base-layer-while-markers-stay-above-xindex-is-ignored – Beni Apr 30 '22 at 22:06
-3

Leaflet maps are good for an interactive display but not for an export. I use ggmap package to display statics maps the can be saved as PNG file.

cosworth
  • 15
  • 1
  • 2
  • since the question includes a reproducible example, please try to give a reproducible answer as well. – loki May 04 '17 at 13:19
  • Why do you say that Leaflet maps aren't good for export? I've seen a lot of examples with leaflet maps generated in R that have been exported –  May 08 '17 at 06:24
  • They are hard to export (many lines of code) in comparison with other "statics" maps, their primary purpose is dynamic display on screen. I would say it's like trying to paper print a GIF but it's a little exaggerated :) – cosworth May 09 '17 at 09:20