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)
Crossposted on Stack Overflow