Search code examples
rshinyr-leaflet

How to read a geojson file containing feature collections to leaflet-shiny directly


My question is how to read a geojson file containing feature collections to leaflet-shiny. I have seen joe's github https://github.com/jcheng5/leaflet-shiny/blob/master/inst/examples/geojson/server.R but he did not use an external dataset but created the geojson manually. i am confused whether

  1. Is that possible to read geojson file to leaflet-shiny directly?
  2. If not, what does the structure of feature collections look like in shiny (in Joe's post it is multi-polygon) and how to create that in a faster and easier way?

Solution

  • You're probably looking to be able to manipulate the GeoJSON file directly in R-Shiny and R as opposed to reading a static file.

    As previously mentioned, you can feed a string containing the GeoJSON to leaflet-shiny such as this GeoJSON FeatureCollection:

    {
      "type": "FeatureCollection",
      "features": [
    {
        "type": "Feature",
        "properties": {"party": "Republican"},
        "id": "North Dakota",
        "geometry": {
            "type": "Polygon",
            "coordinates": [[
                [-104.05, 48.99],
                [-97.22,  48.98],
                [-96.58,  45.94],
                [-104.03, 45.94],
                [-104.05, 48.99]
            ]]
        }
    },
    {
        "type": "Feature",
        "properties": {"party": "Democrat"},
        "id": "Colorado",
        "geometry": {
            "type": "Polygon",
            "coordinates": [[
                [-109.05, 41.00],
                [-102.06, 40.99],
                [-102.03, 36.99],
                [-109.04, 36.99],
                [-109.05, 41.00]
            ]]
        }
    }
      ]
    }
    

    Then you can use RJSONIO::fromJSON to read this object in the format provided in the example and manipulate it in R such as this (Note: it appears that you have to add styles after reading the GeoJSON file as opposed to reading a GeoJSON FeatureCollection file that already has styles):

    geojson <- RJSONIO::fromJSON(fileLocation)
    geojson[[2]][[1]]$properties$style <- list(color = "red",fillColor = "red")
    geojson[[2]][[2]]$properties$style <- list(color = "blue",fillColor = "blue")
    geojson$style <- list(weight = 5,stroke = "true",fill = "true",opacity = 1,fillOpacity = 0.4)
    

    This will give you the same R object if you had just entered this:

    geojson <- list(
      type = "FeatureCollection",
      features = list(         
            list(
              type = "Feature",
              geometry = list(type = "MultiPolygon",
                              coordinates = list(
                                list(
                                  list(
                                    c(-109.05, 41.00),
                                    c(-102.06, 40.99),
                                    c(-102.03, 36.99),
                                    c(-109.04, 36.99),
                                    c(-109.05, 41.00)
                                  )
                                )
                              )
              ),
              properties = list(
                party = "Democrat",
                style = list(
                  fillColor = "blue",
                  color = "blue"
                )
              ),
              id = "Colorado"
            ),
            list(
              type = "Feature",
              geometry = list(type = "MultiPolygon",
                              coordinates = list(
                                list(
                                  list(  
                                    c(-104.05, 48.99),
                                    c(-97.22,  48.98),
                                    c(-96.58,  45.94),
                                    c(-104.03, 45.94),
                                    c(-104.05, 48.99)
                                  )
                                )
                              )
              ),
              properties = list(
                party = "Republican",
                style = list(
                  fillColor = "red",
                  color = "red"
                )
              ),
              id = "North Dakota"
            )
        ),
      style = list(
              weight = 5,
              stroke = "true",
              fill = "true",
              fillOpacity = 0.4
              opacity = 1
            ))