css - Shinydashboard - 根据所选选项卡更改背景

标签 css r shiny shiny-server shinydashboard

我正在使用 Shinydashboard 包创建仪表板,我需要根据所选选项卡更改背景颜色。我已经尝试了以下代码,但它没有按预期工作。

library(shiny)
library(shinydashboard)
library(dplyr)

ui <- dashboardPage(dashboardHeader(dropdownMenuOutput("notificationMenu")), 
                    dashboardSidebar(sidebarMenu(menuItem("Page 1", tabName = "page1"),
                                                 menuItem("Page 2", tabName = "page2"))),
                    dashboardBody(tags$style(".content {background-color: #f7f7f7;
            .content-wrapper .tab-pane .shiny-tab-page1 {background-color: #000000;
            }
            "),
                                  tabItems(
                      tabItem(tabName = "page1", h4("This is Page 1")),
                      tabItem(tabName = "page2", 
                              textInput("text", "Enter News:", "New News."),
                              actionButton("save", "Save")))))

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

  # Intial Header News: 1 Message from Admin
  raw_news$news <- data_frame(from = "Admin", text = "this is a message")

  # The notifications in header
  output$notificationMenu <- renderMenu({
    raw_news <- raw_news$news

    dropdownMenu(
      messageItem(raw_news$from[1], raw_news$text[1])
    )
  })

  # save a new notification
  observeEvent(input$save, {
    raw_news$news <- data.frame(from = "User", text = input$text)
  }) 
}

shinyApp(ui = ui, server = server)

任何帮助将不胜感激。

最佳答案

一种可能的解决方案是根据所选选项卡呈现样式标签。请注意,为此,侧边栏菜单需要一个id。下面是一个工作示例,希望对您有所帮助!


enter image description here

library(shiny)
library(shinydashboard)
ui <- dashboardPage(dashboardHeader(dropdownMenuOutput("notificationMenu")), 
                    dashboardSidebar(sidebarMenu(id='sidebar',
                                                 menuItem("Page 1", tabName = "page1"),
                                                 menuItem("Page 2", tabName = "page2")),
                                     uiOutput('style_tag')),
                    dashboardBody(
                      tabItems(
                        tabItem(tabName = "page1", h4("Blue!",style='color:white')),
                        tabItem(tabName = "page2", h4('Red!'))
                      ))
)

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

  output$style_tag <- renderUI({
    if(input$sidebar=='page1')
      return(tags$head(tags$style(HTML('.content-wrapper {background-color:blue;}'))))

    if(input$sidebar=='page2')
      return(tags$head(tags$style(HTML('.content-wrapper {background-color:red;}'))))
  })
}

shinyApp(ui = ui, server = server)

关于css - Shinydashboard - 根据所选选项卡更改背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49358329/

相关文章:

html - 在仪表板标题中间对齐可变长度的文本

HTML - 即使具有透明度,框阴影也不适用于圆 Angular 正方形

css - 如何将两个 DIV 放置在水平固定标题内并使所有内容垂直对齐?

jquery - Bootstrap Carousel 无法与 asp.net MVC 一起使用

Shiny 中的 R 大型数据表显示

r - R : Reactive Values vs. 全局变量中的 Shiny

javascript - 在不影响正常屏幕尺寸的情况下为屏幕尺寸调用样式表

r - 预测 3 个水平的因子并返回每个因子的百分比

r - 如何在 R 中创建多个新的逻辑列(基于数值变量的截止序列)?

r - 带有 ggplot map 的 Shiny 应用程序 - 多边形颜色与用户输入不匹配