Shiny GroupCheckBoxInput based on two reactive variables


#1

...I'm trying to use two groupcheckbox inputs to filter data set but can't seem to work out how to make it work

server <- function(input, output) {
  
  df_amount_subset <- reactive({
    req(input$selected_category & input$selected_year)
    filter(df_amount, Category %in% input$selected_category & year %in% input$selected_year)
  })

The error message is operations are possible only for numeric, logical or complex types

I'm guessing it's because there are two reactive components to the expression but when I tried to write them separately

  df_amount_year <- reactive({
    req(input$selected_year)
    filter(df_amount, year %in% input$selected_year)
    
  })
  
  df_amount_subset <- reactive({
    req(input$selected_category)
    filter(df_amount_year, Category %in% input$selected_category)
    
  })

this is the error message

no applicable method for 'filter_' applied to an object of class "c('reactiveExpr', 'reactive')"

any help gratefully appreciated, thank you :slight_smile:


#2

I managed to get it to work by filtering in the renderplot function

  output$weekdayPlot <- renderPlot({
    filtered <- df_amount %>% filter(Category %in% input$selected_category & year %in% input$selected_year)
    ggplot(filtered, aes(weekday, TotalAmount, fill = Category)) +
      geom_col()

:smiley:


#3

Your issue with the second implementation in your original post is that you are referencing the reactive object without the required (). I am assuming that df_amount is a predefined variable that is not a reactive. Your reactive's should work if you split them like this:

df_amount_year <- reactive({
    req(input$selected_year)
    filter(df_amount, year %in% input$selected_year)
    
  })
  
  df_amount_subset <- reactive({
    req(input$selected_category)
    filter(df_amount_year(), Category %in% input$selected_category)
    
  })

output$weekdayPlot <- renderPlot({
    ggplot(df_amount_subset(), aes(weekday, TotalAmount, fill = Category)) +
      geom_col()
})

All reactive expressions have to be called with the () at the end because they are essentially functions. You can learn more about it through the shiny reactivity tutorials


#4

ahhhhh, thank you :slight_smile:
that makes sense