Martin Boros
Martin Boros

Reputation: 798

Leaflet Map not rendering in Rmarkdown, R Notebook when opened in browser

For some reason, I can view the leaflet map in the RStudio viewer, but not in a browser, even a very basic map.

```{r, echo=FALSE, warning=FALSE}
library(leaflet)
m <- leaflet() %>% addTiles()
m
```

All other images appear no problem when viewing the HTML file in the browser, but the map won't. Does anybody ran into this problem before?

Upvotes: 5

Views: 2813

Answers (3)

Ina Airapetsyan
Ina Airapetsyan

Reputation: 41

Just open the map in Browser instead of Viewer. The same with shiny.

Upvotes: 0

rm1104
rm1104

Reputation: 341

Reposting from GIS stack exchange, which worked for me:

"The solution is simply reminding leaflet what the basemap is. It defaults to OSM in RStudio output, but then didn't export it with html."

I added (repeated) the following code at the end of the all my leaflet code.

%>% addProviderTiles(providers$CartoDB.Positron)  

Upvotes: 0

Samuel
Samuel

Reputation: 3051

There is something wrong with the default tiles set. Try this:

```{r, echo=FALSE, warning=FALSE}
library(leaflet)
m <- leaflet() %>% addTiles(urlTemplate = 'http://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png')
m
```

You can find a good resource here for alternative third-party tiles.

Upvotes: 2

Related Questions