首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dplyr过滤器从data.frame返回所有

dplyr过滤器从data.frame返回所有
EN

Stack Overflow用户
提问于 2022-10-06 19:55:23
回答 2查看 42关注 0票数 1

我有一个selectInput函数,当用户选择“所有大学”时,我希望dplyr::filter返回所有值。它被传递了一个数据文件,所以dplry::if_else出错了。

代码语言:javascript
复制
    sidebarLayout(
        sidebarPanel(
          selectInput("university", 
                     "Select University",
                     choices = list("All Universities" = "all", "UAA" = "uaa", "UAF" = "uaf", "UAS" = "uas"),
                     multiple = F
          ),
        width = 2),

这是我的闪亮表输出函数

代码语言:javascript
复制
   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)
   }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-06 20:17:44

以下是John的解决方案的“在线”版本。它看起来有点奇怪,因为您使用的是|>管道,它没有位置保持器,所以我使用()()结构和\()匿名函数定义了一个。

代码语言:javascript
复制
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)
   }
票数 2
EN

Stack Overflow用户

发布于 2022-10-06 20:10:47

您可以隔离过滤它的代码,并使用简单的if检查它。

注意:只有当这种情况是孤立的.时,这才有效。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73979389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档