【问题标题】:Shinyalerts: How do I know whether user pressed OK or Cancel?Shinyalerts:我如何知道用户是否按下了确定或取消?
【发布时间】:2019-01-07 23:04:14
【问题描述】:

我正在创建一个允许用户删除一些信息的应用程序。但是,我不想立即删除它,而是想确保被删除的文件是正确的文件。我遇到了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!'
      )

    })
  }
)

【问题讨论】:

    标签: r shiny shinyjs shinyalert


    【解决方案1】:

    您可以使用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)
        })
      }
    )
    

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      相关资源
      最近更新 更多