Empty SpatialPolygonsDataFrame object passed and will be skipped

Hello,

I'm having a problem with the 'reactive' and 'observe' parts of the shiny Server code. The code below will produce a leaflet map but wont show the polygons due to the error. I need some advice on how to construct both reactive and observe code.

Code:-

library(sf)
library(shiny)
library(leaflet)
library(rgdal)
library(spData)
hedra <- rgdal::readOGR("data.geojson")
names(hedra)
hedra <- spTransform(hedra,CRS("+init=epsg:4326"))
plot(hedra,axes=TRUE)
ui <- fluidPage(
  titlePanel("House buying completions between 2015 - 2019"),
   sidebarLayout(
    sidebarPanel(
      helpText("You can create interactive demographic maps for housing completions between the years 2015 and 2019"),
      
      selectInput(inputId = "var",
                  label = "Choose a completion year to display",
                  choices = c("Completions 2019", "Completions 2018",
                              "Completions 2017", "Completions 2016", "Completions 2015",
                              "Completions Total"),
                  selected = "Completions Total"),
      
      sliderInput(inputId = "range",
                  label = "No. of properties:",
                  min = 0, max = 106, value = c(0, 106))
    ),
    mainPanel(leafletOutput(outputId = "map"))
  )
)
server <- function(input, output) {
  
  output$map <- renderLeaflet({
    leaflet() %>% addProviderTiles("OpenStreetMap.HOT", group = "OpenStreetMap.HOT") %>%
      setView(-3.4710, 51.8633, zoom = 9) %>%
        addPolygons(data = hedra[hedra$var < input$var, hedra$range < input$range])})
}

# ShinyApp Function:
shinyApp(ui, server)

Error:

Listening on http://###.##.#.##:####
Warning in polygonData.SpatialPolygonsDataFrame(data) :
  Empty SpatialPolygonsDataFrame object passed and will be skipped

Hi,

Could you use the datapasta package to give us a snippet of the hedra variable. This way I can run the app with some sample code and actually test it.

If you don't know how to use the datapasta, check the details in the reprex example here:

PJ

Hi Pieter,

Thank you for your help been trying to solve this problem for some months. I hope i have cut a sample of the data correctly for you following the instructions for reprexing. It kept throwing up an error: 'R Code execution error' when running 'Addons' > 'reprex selection'. Let me know if there is anything else I need to do to run this process again.

Many thanks,

Paul


tibble::tribble(
ogc_fid, fid, name, completions_2019, completions_2018, completions_2017, completions_2016, completions_2015, completions_Total,
1197, 128, "Darrenfelen", 7, 46, 25, 27, 1, 106,
696, 24, "Glyn Tarell", 0, 1, 1, 0, 0, 2,
1151, 68, "Castle", 0, 10, 1, 0, 0, 11,
1154, 137, "Cantref", 0, 10, 1, 0, 0, 11,
657, 127, "St. David Within", 2, 14, 3, 6, 1, 26,
790, 43, "Talgarth", 4, 22, 1, 0, 1, 28,
674, 23, "Crickhowell", 0, 49, 3, 2, 25, 79,
700, 26, "Hay", 37, 43, 1, 3, 0, 84,
)

Hi,

The tibble was missing ~ in front of the col names so I was able to load it now.

hedra = tibble::tribble(
  ~ogc_fid, ~fid, ~name, ~completions_2019, ~completions_2018, ~completions_2017, ~completions_2016, ~completions_2015, ~completions_Total,
  1197, 128, "Darrenfelen", 7, 46, 25, 27, 1, 106,
  696, 24, "Glyn Tarell", 0, 1, 1, 0, 0, 2,
  1151, 68, "Castle", 0, 10, 1, 0, 0, 11,
  1154, 137, "Cantref", 0, 10, 1, 0, 0, 11,
  657, 127, "St. David Within", 2, 14, 3, 6, 1, 26,
  790, 43, "Talgarth", 4, 22, 1, 0, 1, 28,
  674, 23, "Crickhowell", 0, 49, 3, 2, 25, 79,
  700, 26, "Hay", 37, 43, 1, 3, 0, 84,
)

hedra <- spTransform(hedra, CRS("+init=epsg:4326"))

Unfortunately, the data provided is not working with the first function as generates this error

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘spTransform’ for signature ‘"tbl_df", "CRS"’

Can you please create another dataset with valid data for that function. I think it needs at least coordinates or something.

PJ

Hi Pieter,

Getting a lot of '502 errors' due to file size...there are so many coordinates. I've tried adding just one row but even this exceeded the 32000 characters. Is there another way to pass on the file?

Paul

Hi Paul,

I spent the morning learning about geojson and must say it's interesting and powerful stuff :slight_smile:

Anyway, since you can't give me data, i decided to create my own data using this great online tool:

It generates a geosjon file with polygons (or points) you can draw on a map. Based on the app info, I generated this file:

test.geojson

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
		"var": "Completions 2015"
	  },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -3.8884735107421875,
              51.63847621195153
            ],
            [
              -3.954391479492187,
              51.66872254725265
            ],
            [
              -4.0134429931640625,
              51.62995247363852
            ],
            [
              -3.968124389648438,
              51.615032076937794
            ],
            [
              -3.92486572265625,
              51.61290019118336
            ],
            [
              -3.8884735107421875,
              51.63847621195153
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
		"var": "Completions 2019"
	  },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -3.9214324951171875,
              51.61076820529578
            ],
            [
              -3.8726806640625,
              51.64486796450331
            ],
            [
              -3.895339965820312,
              51.68617954855624
            ],
            [
              -3.9640045166015625,
              51.68830799080076
            ],
            [
              -4.0258026123046875,
              51.659778550134725
            ],
            [
              -4.0264892578125,
              51.62441118472021
            ],
            [
              -3.970184326171875,
              51.6120474088444
            ],
            [
              -3.9214324951171875,
              51.61076820529578
            ]
          ]
        ]
      }
    }
  ]
}

I manually added "var": "Completions 2015" and "var": "Completions 2019" to the properties as this was needed for the app.

The Shiny app

library(sf)
library(shiny)
library(leaflet)
library(rgdal)
library(spData)

hedra <- rgdal::readOGR("test.geojson")
hedra <- spTransform(hedra, CRS("+init=epsg:4326"))
ui <- fluidPage(
  titlePanel("House buying completions between 2015 - 2019"),
  sidebarLayout(
    sidebarPanel(
      helpText("You can create interactive demographic maps for housing completions between the years 2015 and 2019"),
      
      selectInput(inputId = "var",
                  label = "Choose a completion year to display",
                  choices = c("Completions 2019", "Completions 2015"),
                  selected = "Completions 2015"),
      
      sliderInput(inputId = "range",
                  label = "No. of properties:",
                  min = 0, max = 106, value = c(0, 106))
    ),
    mainPanel(leafletOutput(outputId = "map"))
  )
)
server <- function(input, output) {
  
  output$map <- renderLeaflet({
    leaflet() %>% addProviderTiles("OpenStreetMap.HOT", group = "OpenStreetMap.HOT") %>%
      setView(-3.9464950, 51.6267556, zoom = 11) %>%
      addPolygons(data = hedra[hedra$var == input$var,])
    })
}

# ShinyApp Function:
shinyApp(ui, server)

The app will display a different region depending on whether you select Completions 2015 or 2019. The other slider is not functioning yet because I did not start adding data manually to that, but the idea should be exactly the same.

Note: I did change the coordinates and zoom of the map to best fit my example

By looking at this geojson file and my code, I hope you can look at yours and figure our the next steps!

Hope this helps,
PJ

Hi Pieter,

This is just what I wanted and it works perfectly...many thanks to you, Just need to get the 'sliderInput' to connect to the polys.

Hi,

Happy I was able to help. This slider should indeed be a similar principle, it's just the filtering of the data that will be a bit different.

Good luck!
PJ

PS: If your issue had been fully resolved, please mark the post that provided the best solution as the solution

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.