rshinyshinybs

Using addPopover in modules


I am trying to use addPopover from shinyBS and I cannot get it to work in a module. The tooltip works fine in the module but the popover doesn't show up. Also, both tooltip and popover work fine when placed directly in the main server section. Tested with shiny_1.3.2 and shinyBS_0.61 on R 3.6.1 and RStudio 1.2.1568.

library(shiny)
library(shinyBS)

counterButton <- function(id, label = "Counter") {
  ns <- NS(id)
  tagList(
     actionButton(ns("button"), label = label),
     bsTooltip(id = ns("button"), title = "Info about the button"),
     verbatimTextOutput(ns("out"))
  )
}

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

  count <- reactiveVal(0)
  observeEvent(input$button, {
    count(count() + 1)
  })

  output$out <- renderText({
    count()
  })

  addPopover(session, 
             id = "out", 
             title = "Info", 
             content = "More info about the counter field", 
             trigger = "hover")
  count
}

ui <- fluidPage(
  counterButton("counter1", "Counter #1")
)

server <- function(input, output, session) {
  callModule(counter, "counter1")
}

shinyApp(ui, server)

Solution

  • You have to use the session$ns:

    counter <- function(input, output, session) {
    
      ns <- session$ns
    
      count <- reactiveVal(0)
      observeEvent(input$button, {
        count(count() + 1)
      })
    
      output$out <- renderText({
        count()
      })
    
      addPopover(session, 
                 id = ns("out"), 
                 title = "Info", 
                 content = "More info about the counter field", 
                 trigger = "hover")
      #count # what's that?
    }