How can I change my code so that when the user has filtered down to 0 counties, a blank map shows, instead of an error message

I have a running shinyapp here.

Currently, if the user selects a lot of the filters such that there are 0 counties displayed, instead of showing the base layer map, it shows this error message when live:

An error has occurred. Check your logs or contact the app author for clarification.

And this error message on desktop:

invalid 'type' (list) of argument

Is there anything I can change in my code to change this so that just a blank base layer map shows, instead of the error message, when 0 counties are selected?

This is my "server" section of the code:

server <- function(input, output) {
 
  
  
  filtered <- reactive({
    if (input$stateselect == "Select all available states") {
      mydata %>% dplyr::filter(if (input$checkbox1 == TRUE)
        Zfixedbroadband_pct >= 0
        else
          (!is.na(Zfixedbroadband_pct))
      ) %>%
        dplyr::filter(
          if (input$checkbox2==TRUE)
            pct_tot_cov_pop_z >=  0
          else
            (!is.na(pct_tot_cov_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox3==TRUE)
            pct_aging_pop_z >= 0
          else
            (!is.na(pct_aging_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox4==TRUE)
            pct_incarc_pop_z >= 0
          else
            (!is.na(pct_incarc_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox5==TRUE)
            pct_vet_pop_z >= 0
          else
            (!is.na(pct_vet_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox6==TRUE)
            pct_dis_pop_z >= 0
          else
            (!is.na(pct_dis_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox7==TRUE)
            pct_minority_pop_z >= 0
          else
            (!is.na(pct_minority_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox8==TRUE)
            pct_rural_pop_z >= 0
          else
            (!is.na(pct_rural_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox9==TRUE)
            pct_lang_barrier_pop_z >= 0
          else
            (!is.na(pct_lang_barrier_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox10==TRUE)
            pct_ipr_pop_z >= 0
          else
            (!is.na(pct_ipr_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox11==TRUE)
            Znonprofitpercapita >= 0
          else
            (!is.na(FIPS_MAP))
        ) %>%
        dplyr::filter(
          if (input$checkbox12==TRUE)
            Zlibrarymean >= 0
          else
            (!is.na(FIPS_MAP))
        ) %>%
        dplyr::filter(
          if (input$checkbox13==TRUE)
            ZcapitaTVradionews >= 0
          else
            (!is.na(FIPS_MAP))
        ) %>%
        dplyr::filter(
          if (input$checkbox14==TRUE)
            Zsocialmedia >= 0
          else
            (!is.na(FIPS_MAP))
        ) %>%
        dplyr::filter(
          if (input$checkbox15==TRUE)
            ZPERCENTHOUSEHOLDSINORG >= 0
          else
            (!is.na(FIPS_MAP))
        )
    }
    else {
      mydata %>% dplyr::filter (STATEFULL %in% input$stateselect) %>% dplyr::filter(if (input$checkbox1 == TRUE)
        Zfixedbroadband_pct >= 0
        else
          (!is.na(Zfixedbroadband_pct))) %>% dplyr::filter(if (input$checkbox2 == TRUE)
            pct_tot_cov_pop_z >= 0
            else
              (!is.na(pct_tot_cov_pop_z))
          ) %>%
        dplyr::filter(
          if (input$checkbox3==TRUE)
            pct_aging_pop_z >=  0
          else
            (!is.na(pct_aging_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox4==TRUE)
            pct_incarc_pop_z >= 0
          else
            (!is.na(pct_incarc_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox5==TRUE)
            pct_vet_pop_z >=0
          else
            (!is.na(pct_vet_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox6==TRUE)
            pct_dis_pop_z >= 0
          else
            (!is.na(pct_dis_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox7==TRUE)
            pct_minority_pop_z >= 0
          else
            (!is.na(pct_minority_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox8==TRUE)
            pct_rural_pop_z >= 0
          else
            (!is.na(pct_rural_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox9==TRUE)
            pct_lang_barrier_pop_z >= 0
          else
            (!is.na(pct_lang_barrier_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox10==TRUE)
            pct_ipr_pop_z >= 0
          else
            (!is.na(pct_ipr_pop_z))
        ) %>%
        dplyr::filter(
          if (input$checkbox11==TRUE)
            Znonprofitpercapita >= 0
          else
            (!is.na(FIPS_MAP))
        ) %>%
        dplyr::filter(
          if (input$checkbox12==TRUE)
            Zlibrarymean >= 0
          else
            (!is.na(FIPS_MAP))
        ) %>%
        dplyr::filter(
          if (input$checkbox13==TRUE)
            ZcapitaTVradionews >= 0
          else
            (!is.na(FIPS_MAP))
        ) %>%
        dplyr::filter(
          if (input$checkbox14==TRUE)
            Zsocialmedia >= 0
          else
            (!is.na(FIPS_MAP))
        ) %>%
        dplyr::filter(
          if (input$checkbox15==TRUE)
            ZPERCENTHOUSEHOLDSINORG >= 0
          else
            (!is.na(FIPS_MAP))
        )
      
      
    }
    
  })
  
  
  filteredstate <- reactive({
    if (input$stateselect=="Select all available states") {
      mydata %>% aggregate(
        by = list(mydata$STATEFP),
        FUN = unique,
        dissolve = T
      )
    }
    else {
      mydata %>% aggregate(
        by = list(mydata$STATEFP),
        FUN = unique,
        dissolve = T
      ) %>% dplyr::filter (
        STATEFULL %in% input$stateselect
      )
    }
  })
  
  
  
  output$mymap <- renderLeaflet ({
    leaflet() %>% addTiles() %>%
      addPolygons(
        data = filtered(),
        stroke = TRUE,
        weight = 0.5,
        color = "white",
        smoothFactor = 0.2,
        fillOpacity = 0.8,
        fillColor = "blue",
        label =  ~ county_state
      ) %>%  addPolylines(
        data = filteredstate(),
        color = "black",
        opacity = 1,
        weight = 2
      )
  })
  
  
  
  
  
  
  
  output$mytable <-  shiny::renderTable({
    mytabledata <- filtered()
    mytabledata <-
      mytabledata %>% as.data.frame() %>%  select(county_state) %>% rename("List of counties selected:" = "county_state")
    
  })
  
  
  
  
}

Also if anyone has any ideas for how I can use DT::renderDataTable() instead of shiny::renderTable() I would appreciate it. I tried to get my table, which is just a 1 column list of the name (variable county_state) of the selected counties to work in DT but couldn't get it to work.

Thank you in advance for any advice you can provide.

I can't reproduce your code, but my hunch is that the problem occurs when your filtered reactive object is null the renderLeaflet crashes.

I suggest - it may or may not work, I can't check - to use an if clause, and test whether the filtered object makes sense (say inherits from sf, or has nrow greater than zero; you may need to be a bit creative here) - if yes then proceed as you do now, and if not then provide just the leaflet and addTiles (without addPolygons and addPolylines).

Thanks jlacko - I was able to solve things using an if clause as you suggest.

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.