简体   繁体   English

R Shiny - 使用 checkboxGroupInput 过滤 data.frame

[英]R Shiny - filter data.frame with checkboxGroupInput

Trying to filter the data in a data frame depending on what check boxes are checked.尝试根据选中的复选框过滤数据框中的数据。 Take the following example:举个例子:

if (interactive()) {
  library(DT)

  Dish <- c("Apple Pie", "Apple Cake", "Blueberry Pie", "Lemon", "Carrot", "Chocolate")
  DishNum <- c("123", "456", "789", "12", "34", "56")
  data <- data.frame(Dish, DishNum)

  ui <- fluidPage(
    checkboxGroupInput(inputId = "chkDish",
                       label = "",
                       inline = TRUE,
                       choices = c("Apple", "Blue", "Not Apple or Blue"),
                       selected = c("Apple", "Blue", "Not Apple or Blue")
    ),
    DT::dataTableOutput(outputId = "DT_Dish")
  )

  server <- function(input, output, session) {
    output$DT_Dish <- DT::renderDataTable({
      DT::datatable(
        data,
        rownames = FALSE,
        options = list(
          dom = 't',
          searchHighlight = TRUE,
          pageLength = 100,
          scrollX = TRUE
        )
      )
    })
  }
  shinyApp(ui, server)
}

If only the "Apple" box is checked, it would only show the data in the table beginning with "Apple".如果仅选中“Apple”框,则只会显示以“Apple”开头的表格中的数据。 If only the "Blue" box is checked, it would only show the data in the table beginning with "Blue".如果仅选中“蓝色”框,则只会显示表格中以“蓝色”开头的数据。 If only the "Not Apple or Blue" box is checked, it would only show the data in the table NOT beginning with "Apple" or "Blue".如果仅选中“非 Apple 或蓝色”框,则只会显示表中不以“Apple”或“Blue”开头的数据。

If any combination of the buttons are checked, it would filter/show the data appropriately.如果选中任何按钮组合,它将适当地过滤/显示数据。

I understand I'd need to use some wildcard for subsetting the data, but not sure the best way to approach this.我知道我需要使用一些通配符来对数据进行子集化,但不确定解决此问题的最佳方法。 Thanks!谢谢!

One way to approach this is to set up regex filters based on your check boxes.解决此问题的一种方法是根据您的复选框设置正则表达式过滤器。 An if_else statement is included to check for the not apple or blue specific case.包含一个if_else语句来检查not apple 或 blue 特定情况。 Otherwise will use the input$chkDisk character values in the regex.否则将使用正则表达式中的input$chkDisk字符值。 All of the filters are searched with an or ( | ).使用或 ( | ) 搜索所有过滤器。 See if this provides the desired behavior.看看这是否提供了所需的行为。

library(dplyr)

server <- function(input, output, session) {       
  filtered_data <- reactive({
    req(input$chkDish)
    filters <- if_else(input$chkDish == "Not Apple or Blue", "(?!Apple)(?!Blue)", paste0("(", input$chkDish, ")"))
    filter(data, grepl(paste0("^(", paste0(filters, collapse="|"), ")"), Dish, perl = TRUE))
  })

  output$DT_Dish <- DT::renderDataTable({
    DT::datatable(
      filtered_data(),
      rownames = FALSE,
      options = list(
        dom = 't',
        searchHighlight = TRUE,
        pageLength = 100,
        scrollX = TRUE
      )
    )
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM