Shiny: Connect and display multiple filters with pickerInput and DataTable

I am attempting to use the pickerInput to make multiple selections of the different variables. When I make selections, "select all", only one item is displated, i.e. the first row "Mazda RX4" . It seems I am missing something on the server side of things that will connect multiple selections to show more than one row that meets the required criteria when selecting multiple variables and getting more than one row.

Here is my example:


library(shiny)
library(shinyWidgets)
library(DT)
library(dplyr)

mtcars <- mtcars[, c(1:5)]
mpg <- mtcars[,2]
cyl <- mtcars[,3]
disp <- mtcars[,4]
hp <- mtcars[,5]

ui <- fluidPage(
    
    # Application title
    titlePanel("Cars Information"),
    
    sidebarLayout(
        sidebarPanel(
            pickerInput(inputId = "mpg",
                        label = "Choose MPG:",
                        choices = c(unique(mtcars$mpg)),
                        options = list(`actions-box` = TRUE),multiple = T),
            
            pickerInput(inputId = "cyl",
                        label = "Choose a cylinder:",
                        choices = c(unique(mtcars$cyl)),
                        options = list(`actions-box` = TRUE),multiple = T),
            
            pickerInput(inputId = "disp",
                        label = "Choose a Displacement:",
                        choices = c(unique(mtcars$disp)),
                        options = list(`actions-box` = TRUE),multiple = T),
            
            pickerInput(inputId = "hp",
                        label = "Choose a weight:",
                        choices = c(unique(mtcars$hp)),
                        options = list(`actions-box` = TRUE),multiple = T),
            actionButton("view", "View Selection")
        ),
        mainPanel(
            DTOutput(outputId = "table")
        )
        
        
    )
)


# Define server logic 
server <- function(input, output) {
    datasetInput <- eventReactive(input$view,{
        
        datasetInput <- filter(mtcars, mpg == input$mpg, cyl == input$cyl,
                               disp == input$disp, hp == input$hp)
        return(datasetInput)
        
    })
    
    
    
    output$table <- renderDT({datasetInput()
    }, colnames = c("Car Make/Model", "Miles per Gallon", "Cylinders", "Displacement", "Weight"))
}
# Run the application 
shinyApp(ui = ui, server = server)

The way you are using filtering in the server is incorrect, you are comparing the mpg value using an equality (==) to input$mpg (a vector or a list, can't remember now). You should use the %in% to check if that value is in the list.

Additionally, I would check if the selections are empty before filtering. If they are empty I would not take them into account when filtering, which seems a more intuitive behaviour, but that depends on your application.

Hope this helps!

datasetInput <- filter(mtcars, mpg %in% input$mpg, cyl %in% input$cyl,
                           disp %in% input$disp, hp %in% input$hp)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.