简体   繁体   中英

How to select shinydashboard skin dynamically?

I want to reactively select skin of shinydashboard through selectInput.

I have tried the following code, but the dashboard frame disappeared.

library(shiny)

library(shinydashboard)

ui <- uiOutput("ui")

server <- function(input, output, session) {

  shiny.session <- reactiveValues(skin = "blue")

  ui <- function() {
    dashboardPage(dashboardHeader(disable = FALSE),
                  dashboardSidebar(),
                  dashboardBody(),
                  skin = shiny.session$skin)
  }

  output$ui <- renderUI(ui())

}

shinyApp(ui, server)

The skin is determined by the class of the shinydashboard body: <body class="skin-blue" style="min-height: 611px; height: auto;"> .

We can change the class dynamically with a bit of javascript. Here's a small example:

example_app_gif

library(shiny)
library(shinydashboard)

js <- "Shiny.addCustomMessageHandler('change_skin', function(skin) {
        document.body.className = skin;
       });"

ui <- function() {
    dashboardPage(dashboardHeader(),
                  dashboardSidebar(),
                  dashboardBody(
                      tags$head(tags$script(js)),
                      selectInput("skin_color", "Skin Color", 
                                  c("blue", "black", "purple", 
                                    "green", "red", "yellow"))
                  ),
                  skin = "blue")
}

server <- function(input, output, session) {
    observeEvent(input$skin_color, {
        session$sendCustomMessage("change_skin", paste0("skin-", input$skin_color))
    })
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM