Search code examples
rshinydashboard

How to create a checkBoxGroup item under a menuItem or fixed box in shiny dashboard sidebar?


I am creating a shiny dashboard that displays data tables in the body. I am trying to add a sidebar on the side with checkboxgroup that filters the data table. Right now the check boxes show but title and the option names are missing. If I do not use the sidebar and put the checkboxes in the dashboard body it does show. But I am trying to put in sidebar or fixed on the side of the page.

library(shiny)
library(shinydashboard)
library(tidyverse)



df <- mpg


header <- dashboardHeader(
    title = "NSCLC Market Share"
)

body <- dashboardBody(
    
    fluidRow(
        column(width = 9,
               tabBox(width = NULL,
                      title = "MarketShare",
                      id = "tabset1", height = "250px",
                      tabPanel("Incidence", 
                               tableOutput('mpg_tbl'),
                               br(),
                      tabPanel("Prevalence", "Tab content 2")
               )
               
        )
    )
))

sidebar <- dashboardSidebar(box(width = NULL, status = "warning",
                                
                                checkboxGroupInput('modelFilter', "Select model",
                                                   choices = 
                                                       unique(df$model),
                                                   selected = unique(df$model)
                                )),
                            br(),
                            box(width = NULL, status = "warning",
                                uiOutput("classFilter"),
                                checkboxGroupInput('classFilter', "Select class",
                                                   choices = unique(df$class),
                                                   selected = unique(df$class)
                                ))
)



ui <- dashboardPage(
    header,
    sidebar,
    body
)

server = function(input, output) {
    
    filtData <- reactive({
        
        df %>% 
            filter(model %in% input$modelFilter) %>% 
            filter(class %in% input$classFilter ) %>% 
            group_by(manufacturer) %>% 
            summarise(count = n())
        
    })
    
    
    
    
    output$mpg_tbl <- renderTable(
        filtData()
        
        
    )
    
    
}

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

Solution

  • The issue is because of box, if you remove that it works -

    library(shiny)
    library(shinydashboard)
    library(tidyverse)
    
    df <- mpg
    
    header <- dashboardHeader(
      title = "NSCLC Market Share"
    )
    
    body <- dashboardBody(
      
      fluidRow(
        column(width = 9,
               tabBox(width = NULL,
                      title = "MarketShare",
                      id = "tabset1", height = "250px",
                      tabPanel("Incidence", 
                               tableOutput('mpg_tbl'),
                               br(),
                               tabPanel("Prevalence", "Tab content 2")
                      )
                      
               )
        )
      ))
    
    sidebar <- dashboardSidebar(checkboxGroupInput('modelFilter', "Select model",
                                                       choices = 
                                                         unique(df$model),
                                                       selected = unique(df$model)
                                    ),
                                br(),
                                checkboxGroupInput('classFilter', "Select class",
                                                   choices = unique(df$class),
                                                   selected = unique(df$class)
                                )
    )
    
    
    
    ui <- dashboardPage(
      header,
      sidebar,
      body
    )
    
    server = function(input, output) {
      
      filtData <- reactive({
        
        df %>% 
          filter(model %in% input$modelFilter) %>% 
          filter(class %in% input$classFilter ) %>% 
          group_by(manufacturer) %>% 
          summarise(count = n())
        
      })
    
      output$mpg_tbl <- renderTable(
        filtData()
      )
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    enter image description here