3

I have recently been wrapping my head around the leaflet package and have gotten around the basics of adding markers, reading/plotting shapefiles from local source and displaying the final outputs in Shiny.

I am currently trying to understand a bit more how to access geojson files directly from the web since the maps that I create in Shiny aren't being properly displayed when uploaded to shinyapps.io (I think it's because the local files aren't being uploaded with the app itself, need to read a bit more on that)

That being said I found this link which contains the political map of all countries in the world. The added documentation suggest using the following code to obtain the polygons but the suggested method does not seem to be working for me.

library(jsonlite)
json_file <- 'https://datahub.io/core/geo-countries/datapackage.json'
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

I tried using geojson_sf(json_data) to convert the extracted information into an sf but I get the error "Error in UseMethod". From what I have read it seems that the best format to work with spatial information is sf which would allow me to left_join the desired information and add population data to each country by its name.

Can I get a pointer on how to extract the polygons so I can addPolygons() them into my leaflet map?

Example data:

Countries <- structure(list(Canada = 37590000, UnitedStates = 328200000, Mexico = 127600000), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"))

Appreciate any pointers or links to references which can clarify my question. Thanks!

1 Answer 1

1

A couple of points in your question suggest a misunderstanding of what geojson is, and what the functions you're using are supposed to do:

  1. geojson_sf() is designed to work on raw json/geojson. So it won't work on your already-parsed json_data.
  2. You say you're trying to work with geojson files, but the link you give is not geojson

If you go to the link https://datahub.io/core/geo-countries/datapackage.json you'll see it's JSON describing the data, and it gives a path of the actual geojson file

library(sf)

json_file <- 'https://datahub.io/core/geo-countries/datapackage.json'
json_data <- jsonlite::fromJSON(json_file)

## The actual geojson is contained here
geojson <- json_data$resources$path[3]

geojson
# [1] "https://pkgstore.datahub.io/core/geo-countries/countries/archive/23f420f929e0e09c39d916b8aaa166fb/countries.geojson"

So now you know where the geojson is located, you can read it in a couple of different ways:

  1. Using geojsonsf
sf <- geojsonsf::geojson_sf(geojson)
  1. Using sf
sf <- sf::st_read(geojson)

You can / should use sf in this instance because the data is not large and it can easily handle it.

I built geojsonsf for when you need a bit more speed, and for it to handle the cases where the sf geojson reader struggles on large files

2
  • I definitely have a bit of a mix-up when it comes to using JSON hahaha I’ve spent most of my R time working with local data/shapefiles but I am trying to better understand how to read objects from JSON, API’s etc. during this lockdown round. 1. Would I be able to use geojson_sf() if I locate the .geojson file inside the json? i.e geojson_sf(pkgstore.datahub.io/core/geo-countries/countries/archive/…)
    – k3r0
    Commented Aug 20, 2021 at 0:55
  • I found the book Geocomputation with R last night and I started giving that a read hopefully I’ll have a better understanding once I am done with that! Thanks for your response, it was super informative and helpful. Amazing work with the package, will definitely be using it once my file needs increase.
    – k3r0
    Commented Aug 20, 2021 at 0:55

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