Home > Net >  %in% operator not operating as expected with reactive statement of a Shiny app
%in% operator not operating as expected with reactive statement of a Shiny app

Time:12-03

I have a Shiny app that takes a dataset and filters it through several user inputs. To do this, I use selectizeInput functions where the user can select one or many options from a list and then these selections are run through reactive statements to get the desired final dataset. I've noticed recently that this no longer works in one of the places I have the app hosted; this app was built and deployed with Shiny 1.6.0 and it's still working in that location, but it isn't working in another spot that has Shiny 1.7.3. I'm wondering if this may be an issue with newer versions of Shiny. Here is an example where multiple selections causes the resulting table to not populate:

library(shiny)
library(dplyr)

data <- mtcars

ui <- fluidPage(
                fluidRow(
                  column(width = 4, wellPanel(
                    selectizeInput("carb", "carb:", c("All", sort(unique(data$carb))), 
                                   selected = "All", multiple = TRUE,
                                   options = list('plugins' = list('remove_button'), 
                                                  'create' = TRUE, 'persist' = FALSE)))),
                  column(width = 8, wellPanel(tableOutput("table")))
                  )
                )


server <- function(input,output,session){
  process <- reactive({
    req(input$carb) # require some input
    if(input$carb == "All"){data} #pass entire dataset if selected
    else(data %>% dplyr::filter(carb %in% input$carb))}) # will not work with > 1 selected

  output$table <- renderTable({process()})
}

shinyApp(ui = ui, server = server)

Selecting just one value allows everything to work fine, but there's an error about the condition having length > 1 if multiple values are selected. Previously when this worked, I was able to select something like 1,2, and 4 for the carb variable and the resulting table would show all rows with one of those three values. I know the input is getting passed on to the argument by adding a renderTable statement into the server:

output$test <- renderTable({as.data.frame(input$carb)})

However, this isn't working when I'm trying to filter the full dataset. I can run everything when selectizeInput(multiple = FALSE), but ideally it should be equal to TRUE so the user has more functionality.

CodePudding user response:

The problem is not the %in% in the filter. The problem is in the following statement

if(input$carb == "All") {data}

If you want carb to allow for multiple values, you can't test for equality with just "All". If you want to test if All in in the selected options, use something like this instead

if("All" %in% input$carb) {data}

CodePudding user response:

The error comes from input$carb == "All". as soon as you input multiple elements, it results in a vecotr of the same length. you can try either of those:

if (all(input$carb == "All")) # shows all rows if Only All is selected
OR
if ("All" %in% input$carb)) # shows all rows as soon as All is selected

Furthermore, you have to use curly brackets in else. doesn't really matter here but just for syntax consistency

  • Related