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

使用bslib时shinyBS tooltip无法显示,如何解决?

问题解决:bslib布局下shinyBS Tooltip失效的修复方案

问题原因

shinyBS依赖Bootstrap 3实现Tooltip,而bslib默认采用Bootstrap 5,两者的Tooltip底层逻辑不兼容,导致嵌套布局后Tooltip无法正常显示。

解决方案一:强制bslib使用Bootstrap 3

在page_sidebar中指定主题版本为Bootstrap 3,让shinyBS的Tooltip功能恢复正常:

library(shiny)
library(shinyBS)
library(bslib)
shinyApp(
  ui =
    page_sidebar(
      title="Histogram",
      theme = bs_theme(version = 3), # 指定Bootstrap 3版本
      sidebar=sidebar(
        title="Controls",
        sliderInput("bins",
                    label = "Number of bins:",
                    min=1,
                    max=50,
                    value=10),
        bsTooltip("bins", "The wait times will be broken into this many equally spaced bins",
                  "right", options = list(container = "body"))
      ),
      card(
        card_header("Histogram"),
        plotOutput("distPlot")
      )
    ),
  server =
    function(input, output, session) {
      output$distPlot <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    }
)

解决方案二:使用bslib原生Tooltip替代shinyBS

推荐直接使用bslib内置的bs_tooltip()函数,完全适配Bootstrap 5,无需依赖shinyBS:

library(shiny)
library(bslib)
shinyApp(
  ui =
    page_sidebar(
      title="Histogram",
      sidebar=sidebar(
        title="Controls",
        sliderInput("bins",
                    label = "Number of bins:",
                    min=1,
                    max=50,
                    value=10) %>% 
          bs_tooltip(
            title = "The wait times will be broken into this many equally spaced bins",
            placement = "right",
            options = list(container = "body")
          )
      ),
      card(
        card_header("Histogram"),
        plotOutput("distPlot")
      )
    ),
  server =
    function(input, output, session) {
      output$distPlot <- renderPlot({
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    }
)

补充说明

  • 方案二更适配现代Shiny开发,原生组件兼容性更好,后续功能扩展更顺畅。
  • 方案一适合已有大量shinyBS组件的项目,可快速解决兼容性问题,但会限制bslib的Bootstrap 5专属特性。

内容的提问来源于stack exchange,提问作者Carlos J. Daboin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 06:02:02