drazraut
drazraut

Reputation: 71

Selecting input that is filtered based on previously selected inputs

I'm working on a project where table is filtered based on the Inputs provided by the user. There are three selectInput conditions.

For better understanding lets assume the mtcars data. User can first select the number of cylinders, then the user should see a selectInput list of number of gears filtered for given value of cylinder. (**for instance, if number of cylinder is 4, then number of gears should be either 4,3,5 **)

Similarly, after selecting the Number of Cylinders and Number of gears the user must see the value of Transmission type as either 0,1. The table should be updated and filtered based on the selected inputs.

I have tried the given code. Please help me.

#loading libraries
library(tidyverse)
library(shiny)
library(DT)

#using mtcars as dataset
df <- read.csv("https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv")

# Define UI for application
ui <- fluidPage(
  
  # Application title
  titlePanel("Details of Given Cars"),
  
  # Sidebar for input  filter
  sidebarLayout(
    sidebarPanel(
      selectInput("cylinder","Number of Cylinders",unique(df$cyl)),
      selectizeInput("gears","Number of gears",choices = NULL),
      selectizeInput("gearbox","Transmission Type 'AUTO=0'",choices = NULL)
    ),
    
    # Show a table
    mainPanel(
      DT::DTOutput("table")
    )
  )
)

# Define server logic required
server <- function(input, output,session) {
  
  #----reactive calculations
  cyl_sel <- reactive({
    df %>% filter(cyl == input$cylinder)
  })
  
  
  observeEvent(cyl_sel(),{
    updateSelectizeInput(session,"gears", choices = cyl_sel()$gear)
  # })
  
  
  gearbox_sel <- reactive({
    cyl_sel() %>% filter(am == input$gears)
  })
  
  observeEvent(gearbox_sel,{
    updateSelectizeInput(session,"gearbox",choices = gearbox_sel()$am)
  
  
  output$table <- DT::renderDT({
    df %>% filter(cyl == input$cylinder,
                  gear == input$gears)
                 # am== input$gearbox) # commented because output is not shown when uncommented
    })
  })
})
   
}

# Run the application 
shinyApp(ui = ui, server = server)


Sample output (without Transmission Type)

Upvotes: 1

Views: 304

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33510

You could use selectizeGroupUI from library(shinyWidgets) to achive this:

library(datasets)
library(shiny)
library(shinyWidgets)
library(DT)

df <- mtcars

ui <- fluidPage(
  titlePanel("Details of Given Cars"),
  sidebarLayout(
    sidebarPanel(
      selectizeGroupUI(
        id = "my-filters",
        params = list(
          cyl = list(inputId = "cyl", title = "Number of Cylinders:"),
          gear = list(inputId = "gear", title = "Number of gears:"),
          am = list(inputId = "am", title = "Transmission Type 'AUTO=0':")
        ),
        inline = FALSE
      )
    ),
    mainPanel(
      DT::DTOutput("table")
    )
  )
)

server <- function(input, output, session) {
  res_mod <- callModule(
    module = selectizeGroupServer,
    id = "my-filters",
    data = df,
    vars = c("cyl", "gear", "am"),
    inline = FALSE
  )
  output$table <- DT::renderDT(res_mod())
}

shinyApp(ui = ui, server = server)

Upvotes: 2

Related Questions