我有一个selectInput函数,当用户选择“所有大学”时,我希望dplyr::filter返回所有值。它被传递了一个数据文件,所以dplry::if_else出错了。
sidebarLayout(
sidebarPanel(
selectInput("university",
"Select University",
choices = list("All Universities" = "all", "UAA" = "uaa", "UAF" = "uaf", "UAS" = "uas"),
multiple = F
),
width = 2),这是我的闪亮表输出函数
output$headcountTable <- function(){
enroll_report |>
dplyr::filter(struc_uni == input$university)|>
dplyr::select(-struc_uni) |>
dplyr::select(-struc_code) |>
kbl(booktabs = T,
col.names = c("Campus",
"Fall 2021",
"Fall 2022",
"Change",
"%",
"Fall 2021",
"Fall 2022",
"Change",
"%"),
caption = "holder text") |>
add_header_above(c("", "Headcount" = 4, "Credit Hours" = 4)) |>
kable_styling("striped",
full_width = T,
position = "left",
font_size = 12)
}发布于 2022-10-06 20:17:44
以下是John的解决方案的“在线”版本。它看起来有点奇怪,因为您使用的是|>管道,它没有位置保持器,所以我使用()()结构和\()匿名函数定义了一个。
output$headcountTable <- function(){
enroll_report |>
(\(d) if(input$university == "all") d
else dplyr::filter(d, struc_uni == input$university))() |>
dplyr::select(-struc_uni) |>
dplyr::select(-struc_code) |>
kbl(booktabs = T,
col.names = c("Campus",
"Fall 2021",
"Fall 2022",
"Change",
"%",
"Fall 2021",
"Fall 2022",
"Change",
"%"),
caption = "holder text") |>
add_header_above(c("", "Headcount" = 4, "Credit Hours" = 4)) |>
kable_styling("striped",
full_width = T,
position = "left",
font_size = 12)
}发布于 2022-10-06 20:10:47
您可以隔离过滤它的代码,并使用简单的if检查它。
注意:只有当这种情况是孤立的.时,这才有效。
if(input$university != "all"){
enroll_report <- enroll_report %>%
dplyr::filter(struct_uni == input$university)
}
enroll_report |>
dplyr::select(-struc_uni) |>
dplyr::select(-struc_code) |>
kbl(booktabs = T,
col.names = c("Campus",
"Fall 2021",
"Fall 2022",
"Change",
"%",
"Fall 2021",
"Fall 2022",
"Change",
"%"),
caption = "holder text") |>
add_header_above(c("", "Headcount" = 4, "Credit Hours" = 4)) |>
kable_styling("striped",
full_width = T,
position = "left",
font_size = 12)https://stackoverflow.com/questions/73979389
复制相似问题