put a dropdown button on shinydashboard header for theme selection

Viewed 1094

I want to put a dropdown menu on shinydashboard header for dashboard theme change. My shiny app is like below. I could not make the app work. What I got is error message;

Error in FUN(X[[i]], ...) : Expected tag to be of type li

It seems like the dashboard area does not accept those typical shiny widgets? The header area is the best place to put this functionality. Does anyone know how I can make that work? Thanks a lot.

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dashboardthemes)


header <- dashboardHeader(
    title = "Dashboard Demo",
    dropdownButton(

        tags$h3("List of Themes:"),

        radioButtons(inputId = 'theme',
                     label = 'Dashboard Theme',
                     choices = c('blue_gradient', 'boe_website', 'grey_light','grey_dark',
                                 'onenote', 'poor_mans_flatly', 'purple_gradient'),
                     selected = 'grey_dark',
                     inline=FALSE),

        circle = TRUE, status = "primary",
        icon = icon("window-maximize"), width = "300px",

        tooltip = tooltipOptions(title = "Click to change dashboard theme")
    )
)

shinyApp(
    ui = dashboardPage(
        header,
        dashboardSidebar(),
        dashboardBody(
            shinyDashboardThemes(
                theme = input$theme
            ),
        )
    ),
    server = function(input, output) { }
)
1 Answers

You can not put the dropdownButton in the dashboardHeader.

Instead you can put it in the dashboardBody or dashboardSidebar and have it updated like this :

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(dashboardthemes)

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "Dashboard Demo"),
    dashboardSidebar(),
    dashboardBody(
      dropdownButton(
        radioButtons(inputId = 'theme',
                     label = 'Dashboard Theme',
                     choices =  c('blue_gradient', 'boe_website', 'grey_light','grey_dark',
                                  'onenote', 'poor_mans_flatly', 'purple_gradient'))
      ),
      uiOutput("myTheme")
    )
  ),
  server = function(input, output) { 
    output$myTheme <- renderUI( shinyDashboardThemes(theme = input$theme))
    }
)
Related