【问题标题】:ShinyDashboard: Switching between default theme and dark modeShinyDashboard:在默认主题和暗模式之间切换
【发布时间】:2021-12-09 04:46:16
【问题描述】:

我使用this 教程将暗模式切换器引入我的应用程序。 我希望应用程序在未选中复选框时返回默认主题。 我在dashboardthemes 库中找不到“默认”主题,也找不到在shinyDashboardThemeDIY() 函数中使用的默认主题定义。 任何线索将不胜感激。

在一段可重现的代码下方(它在第一阶段完美运行,但当我取消选中复选框时没有任何反应:主题保持黑暗而不是再次变为白色和蓝色)

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

# Functions
uiDarkModeCheckBox <- function()
{
    ns <- NS("moduleChangeTheme")
    checkbox <- tagList(
        checkboxInput(
            inputId = ns("dbxChangeTheme"),
            label = "Dark Mode",
            value = FALSE
        )
    )
    
    return(checkbox)
}

uiChangeThemeOutput <- function()
{
    ns <- NS("moduleChangeTheme")
    themeOutput <- tagList(
        uiOutput(ns("uiChangeTheme"))
    )
    
    return(themeOutput)
}

serverChangeTheme <- function(input, output, session)
{
    observeEvent(
        input$dbxChangeTheme, 
        {
            output$uiChangeTheme <- renderUI({
                if (input$dbxChangeTheme == TRUE) {
                    uiChangeThemeOutput()
                    shinyDashboardThemes(theme = "grey_dark")
                }
            })
        }
    )
}

# UI
ui <- dashboardPage(dashboardHeader(),
                    dashboardSidebar(),
                    dashboardBody(
                        uiChangeThemeOutput(),
                            tabItem(tabName = "inputs_tab",
                                    uiDarkModeCheckBox(),
                            )
                    )

)

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

    callModule(module = serverChangeTheme, id = "moduleChangeTheme")
}

shinyApp(ui, server)

【问题讨论】:

  • 请提供一些您目前尝试过的可重现代码。
  • 添加了一段可重现的代码

标签: r shiny shinydashboard


【解决方案1】:

根据需要设置默认主题defaultTheme = "grey_light"

 uiChangeThemeDropdown <- function(dropDownLabel = "Change Theme", defaultTheme = "grey_light")
  {
    changeThemeChoices <- c(
      "Blue gradient" = "blue_gradient",
      "Flat Red" = "flat_red",
      "Grey light" = "grey_light",
      "Grey dark" = "grey_dark",
      "OneNote" = "onenote",
      "Poor man's Flatly" = "poor_mans_flatly",
      "Purple gradient" = "purple_gradient"
    )

【讨论】:

  • 这并不能解决我的问题。 “grey_light”不是闪亮的默认主题。我正在寻找一种方法来启用在默认主题和“grey_dark”主题之间切换主题,以便用户可以在闪亮的应用程序的一次运行中多次切换它(请参阅我上面发布的一段可重现的代码)。谢谢!
猜你喜欢
  • 2021-05-30
  • 2016-12-27
  • 2020-08-24
  • 2021-09-12
  • 1970-01-01
  • 2022-07-19
  • 2020-10-08
  • 2020-09-12
  • 1970-01-01
相关资源
最近更新 更多