Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can achieve this by adding an observeEvent function that triggers the modal dialog to show up after the first click of the actionButton. Here's an example code:

library(shiny)

ui <- fluidPage(
  actionButton("btn", "Click me"),
  fluidRow(
    column(12,
           tags$div(id = "modalExample", class = "modal fade",
                    tags$div(class = "modal-dialog",
                             tags$div(class = "modal-content",
                                      tags$div(class = "modal-header",
                                               tags$h5(class = "modal-title", "Modal Title"),
                                               tags$button(type = "button", class = "close", "×", 
                                                           data_dismiss = "modal", 
                                                           aria_label = "Close")),
                                      tags$div(class = "modal-body",
                                               tags$p("This is the modal body text."))
                             )
                    )
           )
    )
  )
)

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

  observeEvent(input$btn, {
    if (input$btn == 1) {
      showModal("modalExample")
    }
  })

}

shinyApp(ui, server)

In this code, we added an observeEvent function that listens to the click of the actionButton. If the button is clicked for the first time (input$btn == 1), then the showModal function is called to trigger the modal dialog to show up.