覆盖 R Shiny 模态对话框中的关闭按钮

Override dismiss button in R Shiny modal dialogue

我在 R Shiny 中使用模态对话来获取用户的输入。在此表单中,默认情况下有一个关闭按钮,单击它会关闭表单。我想在单击关闭按钮时添加一个确认弹出窗口 (sweetAlert)。

我也准备好使用 javascript,但我需要 sweetAlert 而不是 windows 警报。我也无法成功生成 windows 警报。

如何覆盖此内置 "dismiss" 按钮的功能?我想在有人点击解雇时显示警告,只有当他们确定时才让他们继续。否则我想让他们留在模态对话中。

感谢任何帮助。

您不需要编写自己的 JS 代码,相反您可能想使用 shinyWidgets package

具体来说,看一下确认对话框: http://shinyapps.dreamrs.fr/shinyWidgets/

编辑Here you can find some examples,例如

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)

这是一个方法。代码相当简单。 -

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)