0

I would like to visualize municipalities in R using Leaflet. This is my code:

library(leaflet)
library(jsonlite)

geojson <- readLines("https://cartomap.github.io/nl/wgs84/gemeente_2020.geojson", warn = FALSE) %>%
  paste(collapse = "\n") %>%
  fromJSON(simplifyVector = TRUE)

map <- leaflet() %>%
       addTiles() %>%   
       addGeoJSON(geojson, weight = 1, color = "grey") %>%
       setView(5.387740, 52.155499, zoom = 7)
map

Alas it is not working. I don't get an error message, but I don't get a map with municipality borders either. Could somebody please point out to me what I am doing wrong?

1 Answer 1

0

The addGeoJSON function is expecting a geojson object but jsolite::fromJSON returns a list. This should work:

library(leaflet)
library(sf)
library(geojsonsf)

url <- "https://cartomap.github.io/nl/wgs84/gemeente_2020.geojson"
sf <- st_read(url)

geojson <- sf_geojson(sf)

map <- leaflet() %>%
       addTiles() %>%
       addGeoJSON(geojson, weight = 1, color = "grey") %>%
       setView(5.387740, 52.155499, zoom = 7)

map
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.