Ask Your Question
0

How can I make the modal dialog show up after the first click of the actionButton?

asked 2021-09-23 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
3

answered 2022-04-14 23:00:00 +0000

lakamha gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-23 11:00:00 +0000

Seen: 11 times

Last updated: Apr 14 '22