Expand/Collapse Shiny selectInput function

2 answers

I would like to find a resource that would allow my Shiny selectInput function to expand/collapse based on the category headings that I have created. I have searched through some bootstrap resources, but am not yet successful. Please forgive my minimal working example, I acknowledge that there may be more efficient ways to provide a MWE. Thanks for any advice you can offer.

library(shiny) library(tidyverse) #create a quick dataset to plot schools <-  as.data.frame(table(     c('Adams', 'Van Buren', 'Clinton', 'Douglas', 'Edwards',                'Franklin', 'Grant', 'Harrison', 'Ignatius', 'Justice',                'Kellogg', 'Lincoln'),      dnn = list("school")))  enrollment <- as.data.frame(table(     c(300, 305, 265, 400, 500, 450, 475, 900, 800, 850, 1200, 1500),      dnn = list("enrollment")))  schoolsDataframe <- schools %>%      bind_cols(enrollment) %>%      select(school, enrollment)  #define data elements for selectInput choices argument elem <- c('Adams', 'Van Buren', 'Clinton', 'Douglas') mid <- c('Edwards', 'Franklin', 'Grant') high <- c('Harrison', 'Ignatius', 'Justice') multi <- c('Kellogg', 'Lincoln')  # Define UI  ui <- fluidPage(     tags$style(".optgroup-header { color: #FFFFFF !important; background: #000000 !important; }"),     # Application title     titlePanel("Expandable selectInput"),      # Sidebar with a select input     sidebarLayout(         sidebarPanel(             selectInput(inputId = 'schoolsInput',                         label = 'Select a school',                         choices = list('Elementary' = elem,                                         'Middle' = mid,                                         'High' = high,                                         'Multi-level' = multi),                          selectize = TRUE)         ),          # Show a plot          mainPanel(            plotOutput("myPlot")         )     ) )  # Define server logic required to draw a plot server <- function(input, output) {      output$myPlot <- renderPlot({         #filter the data based on selectInput schoolsDataframe <- schoolsDataframe %>%      filter(school == input$schoolsInput)         # draw the plot ggplot(data = schoolsDataframe,         mapping = aes(x = school,                       y = enrollment))+     geom_col()     }) }  # Run the application  shinyApp(ui = ui, server = server) 

enter image description here

enter image description here

All answers to this question, which has the identifier 61231769

The best answer:

Here's a start for you, although it may not be exactly what you want. I think you want a dynamic selection list, based on the school type (elementary, middle...). Here's a way you can do that with 2 selection lists, where the lower one is dynamic, responding to the choice in the upper selection list.

I also tried to simplify your data setup. You can copy/paste the code to run it.

library(shiny) library(tidyverse)  #define data elements  schools <- data.frame (schoolName=  c('Adams', 'Van Buren', 'Clinton', 'Douglas', 'Edwards','Franklin', 'Grant', 'Harrison', 'Ignatius', 'Justice', 'Kellogg', 'Lincoln'),                       schoolType = c('Elementary','Elementary','Elementary','Elementary','Middle','Middle','Middle','High','High','High','Multi-level','Multi-level'),                       schoolEnrollment = c(300, 305, 265, 400, 500, 450, 475, 900, 800, 850, 1200, 1500))  # Define UI  ui <- fluidPage(   tags$style(".optgroup-header { color: #FFFFFF !important; background: #000000 !important; }"),   # Application title   titlePanel("Expandable selectInput"),    # Sidebar with a select input   sidebarLayout(     sidebarPanel(       selectInput(inputId = 'schoolType',                   label = 'Select a School Type',                   choices = list('Elementary',                                  'Middle',                                   'High',                                   'Multi-level'),                    ),       selectInput("schoolName", "Select School:","Elementary"),     ),      # Show a plot      mainPanel(       plotOutput("myPlot")     )   ) )  # Define server logic required to draw a plot server <- function(input, output, session) {    # Set up the selection for counties   observe ({     selectionSchoolNames <- sort(unique(unlist(subset(schools$schoolName,schools$schoolType==input$schoolType))))     updateSelectInput(session, "schoolName", choices = selectionSchoolNames)   })    output$myPlot <- renderPlot({     #filter the data based on selectInput     schoolsDataframe <- schools %>%        filter(schoolType == input$schoolType)     # draw the plot     ggplot(data = schoolsDataframe,             mapping = aes(x = schoolName,                           y = schoolEnrollment))+       geom_col()   }) }  # Run the application  shinyApp(ui = ui, server = server) 
library(shiny)  onInitialize <- ' function(){   this.$dropdown_content.on("mousedown", function(e){     e.preventDefault();      return false;   });    $("body").on("click", ".optgroup-header", function(){     $(this).siblings().toggle();   }); }'  onDropdownOpen <- ' function(){   setTimeout(function(){     $(".optgroup .option").hide();   }, 0); }'  shinyApp(    ui = fluidPage(     selectizeInput("state", "Choose a state:",                 list(`East Coast` = list("NY", "NJ", "CT"),                      `West Coast` = list("WA", "OR", "CA"),                      `Midwest` = list("MN", "WI", "IA")),                 options = list(                   onInitialize = I(onInitialize),                   onDropdownOpen = I(onDropdownOpen)                 )     ),     textOutput("result")   ),    server = function(input, output) {     output$result <- renderText({       paste("You chose", input$state)     })   }  ) 

enter image description here

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data