Cannot get bsModal to trigger within Shiny modules

586 Views Asked by At

I am attempting to create and trigger a bsModal in a Shiny app that uses modules and I cannot get it to open with an action button. My initial thought is that it has something to do with the ns() function on the action button ID but I have tried using the trigger ID with and without ns() and neither has worked. I created the example below to simulate the issue.

test_mod_ui <- function(id) {
  ns <- NS(id)
  sidebarLayout(
    sidebarPanel(
      actionButton(ns("test"), "Show Plot")
    ), 
    mainPanel(
      plotOutput(ns("cars")),
      shinyBS::bsModal(
        id = "modal_example",
        title = "Example",
        trigger = ns("test"),
        size = "large",
        plotOutput(ns("cars_modal"))
      )
    )
  )
}

test_mod_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    
    output$cars <- renderPlot(
      pairs(
        ~mpg+disp+drat+wt,
        data=mtcars,
        main="Simple Scatterplot Matrix"
      )
    )
    
    output$cars_modal <- renderPlot(
      pairs(
        ~mpg+disp+drat+wt,
        data=mtcars,
        main="Simple Scatterplot Matrix"
      )
    )
  })  
}


ui <- fluidPage(
  tagList(
    test_mod_ui("test")
  )
)

server <- function(input, output) {
  test_mod_server("test")
}

shinyApp(ui, server)

The action button and the cars_modal plot show up, but the clicking the action button should open a new window with the same plot and it does not.

1

There are 1 best solutions below

1
On

You have to load the shinyBS package.

library(shiny)
library(shinyBS)

......

Then it works fine (and you can remove shinyBS::).