我正在创建一个允许用户删除某些信息的应用程序。但是,我要确保立即删除的文件不是正确的文件,而不仅仅是立即删除它。我碰到了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!'
)
})
}
)
答案 0 :(得分:3)
您可以使用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)
})
}
)
答案 1 :(得分:0)
另一种方法是使用模态。这种方式的优势是您可以在应用程序中很好地向下滑动创建迷你UI。例如:
observeEvent(input$btn, {
showModal(dataModal2())
}
dataModal2 <- function(failed = FALSE) {
modalDialog(
title= h3("WARNING!"),
fluidRow(column(12,align="center",
'Are you sure?.'),
br(),
"You can add different UI widgets"
),
easyClose=FALSE, ## force the click
footer=fluidRow(column=12,align="center",
actionButton("confirmName2", "Confirm",icon=icon("fas fa-check-circle")),
actionButton("confirmName3", "Undo",icon("fas fa-undo", lib = "font-awesome")))
)
}
observeEvent(input$confirmName2, {
## any action
removeModal()
})
observeEvent(input$confirmName3, {
## different action, maybe saving how many times user changed his mind.
removeModal()
})