I am creating an Rshiny app where I give the user the choice to filter the dataframe using check boxes which correspond to a filter row. The user also has the option via radio buttons to control the filter logic to use 'AND' logic or 'OR' logic. I've created a minimum reproducible example using the letters x-z as filter options where rows have every possible combination of x,y, and z. The checkboxes however will only have the option of x, y, or z. If a user has the OR logic selected and all letters, every single row will be visible. If a user has the OR logic selected and only 'x' and 'y', only rows x,y,xy,xyz,xz,yz will be shown. If a user has the same 'x' and 'y' selected but with the AND logic selected, only rows 'xy' and 'xyz' will be shown.
I am having difficulties creating the filtering logic for this and any help will be appreciated. The actual filters have about 10 unique choices which may increase as the complexity of the dataframe increases, and I'd like to make the filter as flexible as possible rather than hard coding every possible combination.
A reproducible example:
df <- data.frame(value = 1:7,
filter = c("x","y","z","xy", "xz", "yz", "xyz"))
library(shiny)
ui <- fluidPage(
title = "Example",
sidebarLayout(
sidebarPanel(
radioButtons("andOR",
label = "Filters Logic:",
c("OR", "AND"),
inline = TRUE),
checkboxGroupInput("filter", "Filter Options:",
letters[24:26], selected = letters[24:26])
),
mainPanel(
DT::dataTableOutput("mytable1")
)
)
)
server <- function(input, output) {
#filtering logic
filteredData <- reactive({
if(input$andOR == 'OR') {
df %>%
filter()
} else {
df %>%
filter()
}
})
output$mytable1 <- DT::renderDataTable({
DT::datatable(filteredData())
})
}
shinyApp(ui, server)