You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny bslib导航栏标题在Shiny Server中显示为黑色的问题排查

问题

我在Shiny中使用bslib创建了一个带有navset bar的仪表盘,代码如下:

library(bslib)
library(shiny)

# Define UI for application that draws a histogram
ui <- page(
  title = "Shiny-template",
  navset_bar(
    title = "Shiny Template",
    bg = "#004646",
    nav_panel("ärenden", 
              page_sidebar(
                # Sidebar with a slider input for number of bins 
                sidebar = sidebar(
                  sliderInput("bins",
                              "Number of bins:",
                              min = 1,
                              max = 50,
                              value = 30)
                ),
                plotOutput("distPlot")
              )
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white',
         xlab = 'Waiting time to next eruption (in mins)',
         main = 'Histogram of waiting times')
  })
}

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

在本地(RStudio Server)运行该应用时,导航栏标题正常显示为白色;但部署到Shiny Server后,标题变为黑色,仅鼠标悬停时才会显示为白色,想请教该异常的可能原因。

可能的原因及解决办法
  • bslib版本不一致:本地与部署环境的bslib包版本存在差异,新版本bslib对导航栏标题的默认样式逻辑做了调整,导致背景色设置后文字颜色未自动适配。
  • Shiny Server全局样式覆盖:Shiny Server加载的全局CSS样式,优先级高于bslib的默认样式,覆盖了标题文字颜色。
  • 自动对比度适配失效:bslib原本会根据导航栏背景色自动计算文字对比度,但部署环境中因CSS渲染顺序或优先级问题,该自动适配逻辑未生效。

快速解决方法

直接通过navset_bar的title_color参数手动指定标题颜色,确保在任何环境下都显示白色:

ui <- page(
  title = "Shiny-template",
  navset_bar(
    title = "Shiny Template",
    bg = "#004646",
    title_color = "white", # 手动指定标题文字颜色
    nav_panel("ärenden", 
              page_sidebar(
                sidebar = sidebar(
                  sliderInput("bins",
                              "Number of bins:",
                              min = 1,
                              max = 50,
                              value = 30)
                ),
                plotOutput("distPlot")
              )
    )
  )
)

版本对齐方案

检查本地与部署环境的bslib版本:

packageVersion("bslib")

将Shiny Server上的bslib包更新至与本地相同版本,也能解决样式不一致问题。

内容的提问来源于stack exchange,提问作者FilipW

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 21:05:20