Search code examples
rshinybslibshinybs

bsPopover doesn't work in bslib layout Shiny


I would like to use a bsPopover hover in a shiny app. This works in the following example:

library(shiny)
library(shinyBS)

ui <- fluidPage(

    titlePanel("Old Faithful Geyser Data"),

    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        mainPanel(
           plotOutput("distPlot"),
           bsPopover(id = "bins", title = "More information", content = "Slider for number of bins", 
                     placement = "bottom", trigger = "hover")
        )
    )
)

server <- function(input, output) {

    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',
             xlab = 'Waiting time to next eruption (in mins)',
             main = 'Histogram of waiting times')
    })
}

shinyApp(ui = ui, server = server)

Output:

enter image description here

Now I would like to combine this with a bslib layout using page_fillable. Unfortunately, after using this layout the hover is gone. Here is some reproducible example:

library(shiny)
library(shinyBS)
library(bslib)

ui <- page_fillable(
  
  titlePanel("Old Faithful Geyser Data"),
  
    card(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    
    card(
      plotOutput("distPlot"),
      bsPopover(id = "bins", title = "More information", content = "Slider for number of bins", 
                placement = "bottom", trigger = "hover")
    )
)

server <- function(input, output) {
  
  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',
         xlab = 'Waiting time to next eruption (in mins)',
         main = 'Histogram of waiting times')
  })
}

shinyApp(ui = ui, server = server)

Output:

enter image description here

As you can see the hover is gone. I don't understand why this happens, so I was wondering if anyone knows how we could still add a hover while using the bslib layout?


Solution

  • Instead of shinyBS you could use e.g. bslib::popover (or bslib::tooltip) to add your popover:

    library(shiny)
    library(bslib)
    
    ui <- page_fillable(
      titlePanel("Old Faithful Geyser Data"),
      card(
        sliderInput("bins",
          "Number of bins:",
          min = 1,
          max = 50,
          value = 30
        ) |> 
          popover(
            "Slider for number of bins",
            title = "More information",
            placement = "bottom",
            options = list(trigger = "hover")
          )
      ),
      card(
        plotOutput("distPlot")
      )
    )
    
    server <- function(input, output) {
      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",
          xlab = "Waiting time to next eruption (in mins)",
          main = "Histogram of waiting times"
        )
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    enter image description here