Shinyalerts:我怎么知道用户是按下了 OK 还是 Cancel?

Gal*_*huk 1 r shiny shinyjs shinyalert

我正在创建一个允许用户删除一些信息的应用程序。但是,我不想立即删除它,而是要确保被删除的文件是正确的文件。我遇到了shinyalerts允许显示“你确定吗?”的包。弹出。但是,我怎么知道用户选择了什么并将其传递给闪亮的?

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!'
      )

    })
  }
)
Run Code Online (Sandbox Code Playgroud)

Big*_*ist 5

您可以使用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)
    })
  }
)
Run Code Online (Sandbox Code Playgroud)