Niam45
Niam45

Reputation: 578

r Shiny: issue filtering data using checkboxes

In my shiny app, I am using checkboxes to filter my data. Consider the example where I have a dataframe with sets and colours: set 1 contains red and yellow colours, set 2 contains only the colour red and set 3 contains only the colour yellow.

When I filter my data by the colours red and yellow at the moment, it returns the set2 (only contains red), set 3(only contains yellow) and set1 that contains both. However, my desired output would be that picking red and yellow would only return the group that contains both colours (set1).

I want the returned group to exactly match my checkbox selection. i.e. checking just red would return the group that only contains red, checking just yellow would return the group that only contains yellow and checking red and yellow returns the group that contains red and yellow.

The code for this example (this is a simplified version of my app) is as follows:

My server:

library(dplyr)
library(shiny)
shinyServer(function(input, output) {

  data <- structure(list(Set = c("Set1", "Set1", "Set1", "Set2", "Set2", 
                             "Set2", "Set3", "Set3", "Set3", "Set4"), Colour = c("red", "red", 
                                                                                 "yellow", "red", "red", "red", "yellow", "yellow", "yellow", 
                                                                                 "blue")), class = "data.frame", row.names = c(NA, -10L))

  output$choose_colour <- renderUI({

    colour.names <- as.vector( unique(data$Colour ))

    checkboxGroupInput("colours", "Choose Colours to Include", 
                       choices  = colour.names)
  })

  model.data <- reactive({

    filtered_data <- data %>% group_by(Set) %>% filter(all(Colour%in% input$colours))
    filtered_data

  })

  output$filteredData <- renderDataTable({
    filtered_data <- model.data()
    filtered_data
  })

})

My ui:

ui <- fluidPage(


 mainPanel(
  uiOutput('choose_colour'),
  
 
  dataTableOutput('filteredData')
  
)
)

Upvotes: 0

Views: 164

Answers (1)

Bruno
Bruno

Reputation: 4151

I switched the order around and changed any for all

library(dplyr)
library(shiny)
library(purrr)
server <- shinyServer(function(input, output) {
  
  data <- structure(list(Set = c("Set1", "Set1", "Set1", "Set2", "Set2", 
                                 "Set2", "Set3", "Set3", "Set3", "Set4"), Colour = c("red", "red", 
                                                                                     "yellow", "red", "red", "red", "yellow", "yellow", "yellow", 
                                                                                     "blue")), class = "data.frame", row.names = c(NA, -10L))
  
  output$choose_colour <- renderUI({
    
    colour.names <- as.vector( unique(data$Colour ))
    
    checkboxGroupInput("colours", "Choose Colours to Include", 
                       choices  = colour.names)
  })
  
  model.data <- reactive({
    
    filtered_data <- data %>% group_by(Set) %>% filter(all(Colour %in% input$colours) & all(input$colours %in%Colour))
    filtered_data
    
  })
  
  output$filteredData <- renderDataTable({
    filtered_data <- model.data()
    filtered_data
  })
  
})

ui <- fluidPage(
  
  
  mainPanel(
    uiOutput('choose_colour'),
    
    
    dataTableOutput('filteredData')
    
  )
)

shinyApp(ui,server)

Upvotes: 1

Related Questions