I have a selectInput function, and when the users select "All Universities" I want dplyr::filter to return all values instead. It's being passed a dataframe so dplry::if_else errors out.
sidebarLayout(
sidebarPanel(
selectInput("university",
"Select University",
choices = list("All Universities" = "all", "UAA" = "uaa", "UAF" = "uaf", "UAS" = "uas"),
multiple = F
),
width = 2),
and here is my shiny table output function
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)
}
CodePudding user response:
Here is an "in-line" version of the solution by John Manacup. It looks a little weird because you are using the |>
pipe, which does not have a place holder, so I define one using the ()()
structure and the \()
anonymous function.
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)
}
CodePudding user response:
You can isolate the code that filters it and check it using a simple if
.
NOTE: this only works if such case is isolated.
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)