0

I have a shiny app and within that app, I have a table that I render based on input from the user. Within that pipe on eviction_county_2010 I am attempting to return the table with only a sample of counties in the same cluster as input$county. I figured out how to do this with multiple lines of code and reassignments, but shiny throws an error whenever I do this as it clearly isn't allowed. How can I tweak my code to return this within one pipe?

Code with multiple assignments. When I attempted this, I got Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

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

  ec <- eviction_county_2010 %>%
    filter(parent_location == input$state) %>%
    filter(name == input$county)
  sel_clust <- c(unique(ec$cluster))
  sel_geoid <- c(unique(ec$GEOID))
  sim_cty <- eviction_county_2010 %>% filter(cluster == sel_clust | GEOID != sel_geoid)
  sim_cty <- unique(sim_cty$GEOID)
  sim_cty <- sample(sim_cty, 5)
  sim_cty <- append(sel_geoid, sim_cty)
...

output$table <- renderTable({eviction_county_2010 %>% filter(GEOID %in% sim_cty)})

I attempted the above code in a reactive, and got Error: 'match' requires vector arguments.

sim_cty <- reactive({ec <- eviction_county_2010 %>%
    filter(parent_location == input$state) %>%
    filter(name == input$county)
  sel_clust <- c(unique(ec$cluster))
  sel_geoid <- c(unique(ec$GEOID))
  sim_cty <- eviction_county_2010 %>% filter(cluster == sel_clust | GEOID != sel_geoid)
  sim_cty <- unique(sim_cty$GEOID)
  sim_cty <- sample(sim_cty, 5)
  sim_cty <- append(sel_geoid, sim_cty)})

  output$table <- renderTable(
    eviction_county_2010 %>% 
      filter(GEOID %in% sim_cty)

Current code

library(shiny)
library(tidyverse)
library(datasets)
library(lubridate)
library(stringr)

eviction_county_2010 <- read.csv("./eviction_county_2010.csv")

ui <- fluidPage(
  sliderInput(inputId = "year",
              label = "Select a Year:",
              min = 2010,
              max = 2016,
              value = 2010,
              step = 1),

  radioButtons(inputId = "layer",
               label = "Select a Dataset to View:",
               choices = c("Eviction Filing Rate"="eviction_filing_rate", "Percent Rent Burden"="rent_burden",
                           "Percent Renter Occupied"="pct_renter_occupied", "Poverty Rate"="poverty_rate")),

  selectInput(inputId = "state",
              label = "Select a State:",
              eviction_county_2010$parent_location),
  selectInput(inputId = "county",
              label = "Select a County:",
              choices = NULL),
  mainPanel(
    h2("Comparisons Across Similar Counties"),
    tableOutput('table')
  )
)

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

  observe({
    x <- filter(eviction_county_2010,parent_location == input$state) %>%
      select(name)
    updateSelectInput(session,"county","Select a County:",choices = unique(x))}
  )

  output$table <- renderTable(
    eviction_county_2010 %>% 
      group_by(County) %>% 
      summarise_at(c("GEOID", "population", "cluster", "poverty_rate", "unemployment_rate", "pct_renter_occupied",  
                     "Percent_Rural", "median_gross_rent", "median_household_income", 
                     "median_property_value", "rent_burden", "pct_white", "pct_nonwhite", 
                     "pct_af_am", "pct_hispanic", "pct_am_ind", "pct_asian", 
                     "pct_nh_pi", "pct_multiple", "pct_other"), mean, na.rm = TRUE) %>% 
      rename(Population = population, `Poverty Rate` = poverty_rate, 
             `Unemployment Rate` = unemployment_rate, `% Renter Occupied` = pct_renter_occupied,  
             `% Rural` = Percent_Rural, `Median Gross Rent` = median_gross_rent, `Median Household Income` = median_household_income, 
             `Median Property Value` = median_property_value, `Rent Burden` = rent_burden, 
             `% White` = pct_white, `% Non White` = pct_nonwhite, 
             `% African American` = pct_af_am, `% Hispanic` = pct_hispanic, `% American Indian` = pct_am_ind, 
             `% Asian` = pct_asian, 
             `% Native Hawaiian/Pacific Islander` = pct_nh_pi, `% Multiple` = pct_multiple, `% Other` = pct_other) # %>%
      # filter(County == str_c(input$county, input$state, sep = ", "))
  )
}

# Run the application 
shinyApp(ui = ui, server = server)
carousallie
  • 776
  • 1
  • 7
  • 25

1 Answers1

0

I can't test it without an example/toy data (see here to learn how to create one). But, per your code, when you convert sim_cty to reactive, you need to call it as a function sim_cty():

output$table <- renderTable(
    eviction_county_2010 %>% 
      filter(GEOID %in% sim_cty())

See here to learn more about reactives in shiny.

MalditoBarbudo
  • 1,815
  • 12
  • 18