我正在尝试创建一个具有多个输入(以其他选择为条件)的Shiny Dashboard,事实证明这相当困难。我可以满足我90%的需求,但这只是我认为可以被代码中嵌入的if/else if/else
语句覆盖的一个错误。
首先这是数据,我们将其称为sample_data
:
+----------+--------+------+--------+
| district | region | turf | counts |
+----------+--------+------+--------+
| 1 | 5 | 5A | 654 |
| 1 | 5 | 5B | 925 |
| 2 | 6 | 6A | 763 |
| 2 | 6 | 6B | 454 |
| 3 | 7 | 7A | 765 |
| 3 | 7 | 7B | 737 |
| 4 | 8 | 8A | 231 |
| 4 | 8 | 8B | 661 |
+----------+--------+------+--------+
districts <- c('All',unique(sort(sample_data$district)))
regions <- c('All',unique(sort(sample_data$region)))
turfs <- c('All',unique(sort(sample_data$turf)))
现在用于UI.r文件:
ui <- dashboardPage(
dashboardHeader(title = "XXXXXX"),
dashboardSidebar(sidebarMenu(
menuItem(
"Dashboard",
tabName = "dashboard",
icon = icon("dashboard")
),
menuItem("Widgets", tabName = "widgets", icon = icon("th")),
uiOutput("Choose_district"),
uiOutput("Choose_region"),
uiOutput("Choose_turf")
)),
dashboardBody(tabItems(
tabItem(
tabName = "dashboard",
fluidRow(
infoBoxOutput("Count_n")
)
)
)
))
最后,这是server.r文件:
server <- function(input, output, session) {
output$Choose_district <- renderUI({
selectizeInput(inputId = "districts",
label = "Select a District",
choices = districts,
selected = districts[1],
multiple = TRUE)
})
output$Choose_region <- renderUI({
selectizeInput(inputId = "regions",
label = "Select a Region",
choices = c("All",c(unique(sort(as.character(sample_data$region[sample_data$district == input$districts]))))),
selected = regions[1],
multiple = TRUE)
})
output$Choose_turf <- renderUI({
selectizeInput(inputId = "turfs",
label = "Selection a Turf",
choices = c("All",c(unique(sort(as.character(sample_data$turf[sample_data$region == input$regions]))))),
selected = turfs[1],
multiple = TRUE)
})
base_matrix <- reactive({
filtered_df <- sample_data
if(input$districts == "All" && input$regions == "All" && input$turfs == "All"){
filtered_df
}
else if(input$districts != "All" && input$regions == "All" && input$turfs == "All") {
filtered_df <- filter(filtered_df , district %in% input$districts)
}
else if(input$districts != "All" && input$regions != "All" && input$turfs == "All") {
filtered_df <- filter(filtered_df, district %in% input$districts & region %in% input$regions)
}
else if(input$districts != "All" && input$regions != "All" && input$turfs != "All") {
filtered_df <- filter(filtered_df, district %in% input$districts & region %in% input$regions & turf %in% input$turfs)
}
else if(input$districts == "" && input$regions == "" && input$turfs == "") {
filtered_df <- filter(filtered_df, district %in% input$districts & region %in% input$regions & turf %in% input$turfs)
}
else if(input$districts != "All" && input$regions != "All" && input$turfs != "All") {
filtered_df <- filter(filtered_df, district %in% input$districts & region %in% input$regions & turf %in% input$turfs)
}
else {
filtered_df
}
})
output$Count_n <- renderInfoBox({
infoBox(
"All Modeled",
base_matrix() %>% dplyr::summarize(totals = sum(counts)),
icon = icon("sort", lib = "glyphicon"),
color = "maroon",
width = 0.5
)
})
}
对于初学者来说,当我启动该应用程序时,所有内容看起来都很干净。该信息框将显示正确的计数5190。但是,当我开始使用过滤器时,它会引发错误missing value where TRUE/FALSE needed
,但我不明白为什么。最终的else
语句是否不包含if和if else初始语句系列中未解决的任何结果?