在 R Shiny 工作。我试图通过 selectInput() 小部件过滤一些数据,通过 textOutput 显示。当用户选择一个县时,贫困百分比应显示在 absolutePanel() 上。我最不擅长过滤数据。我无法弄清楚我做错了什么?我在 Shiny Gallery 中查看了一些 Shiny 应用程序示例,并浏览了 StackOverflow,但我无法查看我的函数有什么问题。我的整个方法是错误的,还是语法错误?如果您有任何线索,建议或资源,请按我的方式拍摄!(我看过的一些对我有帮助的页面:Filter data frame in Shiny app,https : //community.rstudio.com/t/reactive-filtering-and-adding-in-shiny/47739/2,https ://github。)
library(shiny)
library(shinydashboard)
library(tidyr)
library(dplyr)
用户界面
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")
)
) )
服务器.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)
为了使其可重现,这是我的数据:
##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")