Shinyalerts:我怎么知道用户是按了确定还是取消?

Shinyalerts: How do I know whether user pressed OK or Cancel?

我正在创建一个允许用户删除某些信息的应用程序。但是,我不想立即删除它,而是想确保被删除的文件是正确的文件。我遇到了允许显示 "are you sure?" 弹出窗口的 shinyalerts 包。但是,我如何知道用户选择了什么并将其传递给 shiny?

library(shiny)
library(shinyalert)

shinyApp(
  ui = fluidPage(
    useShinyalert(),  # Set up shinyalert
    actionButton("btn", "Delete")
  ),
  server = function(input, output) {
    observeEvent(input$btn, {
      shinyalert(
        title = "Are you sure you want to delete this file?",
        text = "You will not be able to recover this imaginary file!",
        type = "warning",
        showCancelButton = TRUE,
        confirmButtonCol = '#DD6B55',
        confirmButtonText = 'Yes, delete it!'
      )

    })
  }
)

您可以使用 callbackR() 例如将其存储在 reactiveValue()(命名为全局)中:callbackR = function(x) global$response <- x.

完整的应用程序如下:

library(shiny)
library(shinyalert)

shinyApp(
  ui = fluidPage(
    useShinyalert(),  # Set up shinyalert
    actionButton("btn", "Delete")
  ),
  server = function(input, output) {
    global <- reactiveValues(response = FALSE)

    observeEvent(input$btn, {
      shinyalert(
        title = "Are you sure you want to delete this file?",
        callbackR = function(x) {
          global$response <- x
        },
        text = "You will not be able to recover this imaginary file!",
        type = "warning",
        showCancelButton = TRUE,
        confirmButtonCol = '#DD6B55',
        confirmButtonText = 'Yes, delete it!'
      )
      print(global$response)
    })
  }
)