Working in R Shiny. I'm trying to filter some data through a selectInput() widget, displayed through a textOutput. When a user chooses a county, the poverty percentage should display on the absolutePanel(). I'm the worst at filtering data. I can't figure out what I'm doing wrong? I've looked at a few Shiny app examples in the Shiny Gallery, and I've browsed through StackOverflow, but I'm having trouble seeing what's wrong with my functions. Is my whole approach wrong, or is it a syntax error? If you have any clues, suggestions, or resources, please shoot them my way! (Some of the pages I've looked at to help me: Filter data frame in Shiny app, https://community.rstudio.com/t/reactive-filtering-and-adding-in-shiny/47739/2, https://github.com/eparker12/nCoV_tracker)
library(shiny)
library(shinydashboard)
library(tidyr)
library(dplyr)
UI.R
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("selectstate", "Select State", unique(df$Province_State)),
selectInput("selectcounty", "Select County", choices = NULL)
),
dashboardBody(
absolutePanel (id = "controls1",
class = "panel panel-default",
width = 330, height = 200,
textOutput("us_county_poverty")
)
) )
server.R
server <- function(session, input, output) {
########### SELECT STATES AND COUNTIES FUNCTIONS (SelectInput)
observeEvent(
input$selectstate,
updateSelectInput(session,"selectcounty", "Select County",
choices = unique(df$Admin2[df$Province_State==input$selectstate]))
)
##SHOW COUNTY POVERTY (renderText through selectcounty input)
subset_county_poverty <-reactive({
counties1 %>% filter(NAME==input$selectcounty)
})
output$us_county_poverty <- renderText({
#render counties1$All.Ages.In.Poverty.Percent WHERE counties1$NAME==input$selectcounty
paste0(subset_county_poverty()$All.Ages.In.Poverty.Percent)
})
}
########################################################################################
shinyApp(ui = ui, server = server)
To make it reproducible, here's my data:
##DATA:
##COUNTIES - polygons
#> dput(head(counties1))
structure(list(NAME = c("Abbeville-SC", "Accomack-VA", "Ada-ID",
"Adair-IA", "Adair-KY", "Adair-MO"), Year = c(2018L, 2018L, 2018L,
2018L, 2018L, 2018L), ID = c(45001L, 51001L, 16001L, 19001L,
21001L, 29001L), STATE_NAME.x = c("SC", "VA", "ID", "IA", "KY",
"MO"), All.Ages.in.Poverty.Percent = c("19.1", "17.3", "9.7",
"9.7", "23.8", "23.9"), GEOID = c("45001", "51001", "16001",
"19001", "21001", "29001"), ALAND = c("1270337025", "1163743170",
"2724902770", "1474404199", "1049678094", "1469362052"), AWATER = c("53126680",
"2229242744", "21961614", "2597996", "18430783", "5468507"),
INTPTLAT = c("+34.2138089", "+37.7659435", "+43.4514767",
"+41.3285283", "+37.1055589", "+40.1906655"), INTPTLON = c("-082.4604603",
"-075.7578073", "-116.2443760", "-094.4781643", "-085.2813796",
"-092.6035922"), X = c(-82.459, -75.757, -116.241, -94.471,
-85.281, -92.601), Y = c(34.223, 37.765, 43.451, 41.331,
37.104, 40.191)), row.names = c(NA, 6L), class = "data.frame")
##df - csv data
#> dput(head(df))
structure(list(FIPS = c(1001, 1001, 1001, 1001, 1001, 1001),
Admin2 = c("Autauga-AL", "Autauga-AL", "Autauga-AL", "Autauga-AL",
"Autauga-AL", "Autauga-AL"), Province_State = c("AL", "AL",
"AL", "AL", "AL", "AL"), Lat = c(32.53952745, 32.53952745,
32.53952745, 32.53952745, 32.53952745, 32.53952745), Long_ = c(-86.64408227,
-86.64408227, -86.64408227, -86.64408227, -86.64408227, -86.64408227
), Combined_Key = c("Autauga, Alabama, US", "Autauga, Alabama, US",
"Autauga, Alabama, US", "Autauga, Alabama, US", "Autauga, Alabama, US",
"Autauga, Alabama, US"), date = structure(c(18284, 18285,
18286, 18287, 18288, 18289), class = "Date"), value = c(0,
0, 0, 0, 0, 0)), row.names = 782:787, class = "data.frame")