使用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
相关产品推荐
相关产品推荐

