5

I am using modal-dialogue in R Shiny to get input from the user. In this form, there is a dismiss button by default which closes the form when it is clicked. I want to add a confirmation popup (sweetAlert) when the dismiss button is clicked.

I am ready to use javascript as well but i need sweetAlert instead of the windows alert. I was not able to successfully generate a windows alert as well.

How do i override the functionality of this in-built "dismiss" button? I want to show a warning when someone clicks on dismiss and let them continue only if they are sure. Otherwise i want to let them stay on the modal-dialogue.

Any help is appreciated.

Sudhakar Samak
  • 389
  • 4
  • 15

2 Answers2

6

Here's a way. Code is fairly simple. -

library(shiny)

ui <- fluidPage(
  actionButton("show", "Show Modal")
)

server <- shinyServer(function(input, output, session) {
  observeEvent(input$show, {
    showModal(
      modalDialog(
        "some messsage", title = "modal", footer = actionButton("confirm", "Close")
      )
    )
  })

  observeEvent(input$confirm, {
    showModal(
      modalDialog(
        "are you sure?",
        footer = tagList(
          actionButton("yes", "Yes"),
          modalButton("No")
        )
      )
    )
  })

  observeEvent(input$yes, {
    removeModal()
    # do something after user confirmation
  })
})

shinyApp(ui, server)
Shree
  • 10,835
  • 1
  • 14
  • 36
  • 1
    This was exactly what i was looking for. the footer argument did the trick for me. It overrides the default dismiss button. Thanks a lot for the right and quick answer :) – Sudhakar Samak Aug 14 '19 at 19:50
1

You don't need to write your own JS code, instead you might want to use the shinyWidgets package

Specifically, have a look at the Confirmation dialog: http://shinyapps.dreamrs.fr/shinyWidgets/

Edit: Here you can find some examples, e.g.

library("shiny")
library("shinyWidgets")


ui <- fluidPage(
    tags$h1("Confirm sweet alert"),
    actionButton(
        inputId = "launch",
        label = "Launch confirmation dialog"
    ),
    verbatimTextOutput(outputId = "res"),
    uiOutput(outputId = "count")
)

server <- function(input, output, session) {
    # Launch sweet alert confirmation
    observeEvent(input$launch, {
        confirmSweetAlert(
            session = session,
            inputId = "myconfirmation",
            type = "warning",
            title = "Want to confirm ?",
            danger_mode = TRUE
        )
    })

    # raw output
    output$res <- renderPrint(input$myconfirmation)

    # count click
    true <- reactiveVal(0)
    false <- reactiveVal(0)
    observeEvent(input$myconfirmation, {
        if (isTRUE(input$myconfirmation)) {
            x <- true() + 1
            true(x)
        } else {
            x <- false() + 1
            false(x)
        }
    }, ignoreNULL = TRUE)
    output$count <- renderUI({
        tags$span(
            "Confirm:", tags$b(true()),
            tags$br(),
            "Cancel:", tags$b(false())
        )
    })
}

shinyApp(ui, server)
Thomas
  • 1,252
  • 6
  • 24