0

I've put together a script that filters a bunch of things. But whenever I run it I get this error: st_geometry' applied to an object of class "character"

What's bizarre is if I run it outside of my shiny app, like this, it works fine:

mapView(nv %>% st_sf() %>% st_buffer(0) %>% group_by(super_region, region) %>% summarize(geometry = st_union(geometry)))

But when I run it in the script below, it returns that error. What am I doing incorrectly?

 library(tigris)
 library(mapview)
 library(leaflet)
 library(sf)
 library(dplyr)
 library(shiny)
 library(shinyWidgets)

**nv <- [Download shapefile here: https://github.com/gooponyagrinch/blob/blob/master/testdata.shp]**

sr <- nv %>% st_sf() %>% st_buffer(0) %>% group_by(super_region) %>% summarize(geometry = st_union(geometry))
sr_region <- nv %>% st_sf() %>% st_buffer(0) %>% group_by(super_region, region) %>% summarize(geometry = st_union(geometry))
region_turf <- nv %>% st_sf() %>% st_buffer(0) %>% group_by(region, turf) %>% summarize(geometry = st_union(geometry))
turf_area <- nv %>% st_sf() %>% st_buffer(0) %>% group_by(turf, areas) %>% summarize(geometry = st_union(geometry))
area <- nv %>% st_sf() %>% st_buffer(0) %>% group_by(areas) %>% summarize(geometry = st_union(geometry))

ui <- fluidPage(
  fluidRow(
    column(8,
           width = 10, offset = 1,
           tags$h3("Select Area"),
           panel(
             selectizeGroupUI(
               id = "filters",
               params = list(
                 SR = list(inputId = "super_region", title = "Super Region:"),
                 Reg = list(inputId = "region", title = "Region:"),
                 Turf = list(inputId = "turf", title = "Turf"),
                 Areas = list(inputId = "areas", title = "Areas:")
               ))
           ),
           leafletOutput("test")
    )
  )
)

server <- function(input, output, session) {

  res_mod <- callModule(
    module = selectizeGroupServer,
    id = "filters",
    data = nv,
    vars = c('super_region', 'region', 'turf', 'areas')
  )


map_data <- reactive({

  res <- ifelse(!is.null(input$areas), area %>% filter(areas %in% input$areas),
         ifelse(!is.null(input$turf), turf_area %>% filter(turf %in% input$turf),
                ifelse(!is.null(input$region), region_turf %>% filter(region %in% input$region),
                       ifelse(!is.null(input$super_region), sr_region %>% filter(super_region %in% input$super_region),
                              sr))))

})

output$test <- renderLeaflet({

  res <- map_data()

  mapview(res)@map

})

}  

shinyApp(ui,server)

Again, if I want to run say, the object called sr_region outside of Shiny, it runs fine, it's just when I run it as part of the larger app.

What is going wrong?

camille
  • 16,432
  • 18
  • 38
  • 60

1 Answers1

0

Just replace ifelse() function by if ... else ... control like this:

if(!is.null(input$areas))
    res <- area %>% filter(areas %in% input$areas)
else
    if(!is.null(input$turf))
        res <- turf_area %>% filter(turf %in% input$turf)
    else
        if(!is.null(input$region))
            res <- region_turf %>% filter(region %in% input$region)
        else
            if(!is.null(input$super_region))
                res <- sr_region %>% filter(super_region %in% input$super_region)
            else
                res <- sr
HubertL
  • 19,246
  • 3
  • 32
  • 51