Search code examples
rshinyshinybs

R Shiny: Present a ShinyBS Modal Popup on page visit (no user action)


I used bsModal successfully in my code before. However, I can't seem to get a modal pop up to show just when the user visits an app's first page by default. I thought something like this would work, but not. Any idea how I can trigger a bsModal on page visit?

library(shiny)
library(shinyBS)

ui <- fluidPage(
  mainPanel(
    bsModal(id = 'startupModal', title = 'Dum Dum', trigger = '',
            size = 'large', p("here is my mumbo jumbo")),
    width = 12
  )
)

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

}

shinyApp(ui = ui, server = server)

I simply need to alert the user with a message when they visit the app and then allow them to close the modal pop up and navigate the rest of the app freely. I am using Shinydashboard. So, eventually, this has to work with that.


Solution

  • You can use toggleModal to manually trigger the popup from the server.

    library(shiny)
    library(shinyBS)
    
    ui <- fluidPage(
      mainPanel(
        bsModal(id = 'startupModal', title = 'Dum Dum', trigger = '',
                size = 'large', p("here is my mumbo jumbo")),
        width = 12
      )
    )
    
    server <- function(input, output, session) {
      toggleModal(session, "startupModal", toggle = "open")
    }
    
    shinyApp(ui = ui, server = server)