admin管理员组

文章数量:1405387

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.

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.

Share Improve this question asked Aug 13, 2019 at 20:22 Sudhakar SamakSudhakar Samak 3996 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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)

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)

本文标签: javascriptOverride dismiss button in R Shiny modal dialogueStack Overflow