我正在使用模式-对话在R发亮,以获得用户的输入。在此表单中,默认情况下有一个“关闭”按钮,该按钮在单击窗体时关闭该窗体。当单击“解散”按钮时,我想添加一个确认弹出(sweetAlert)。
我也准备好使用javascript,但是我需要sweetAlert而不是windows警报。我也无法成功地生成窗口警报。
我如何覆盖这个内置的“解散”按钮的功能?我想显示警告时,有人点击解散,让他们继续只有当他们是确定的。否则我想让他们继续进行对话。
任何帮助都是非常感谢的。
发布于 2019-08-13 20:39:17
这里有个办法。代码相当简单。-
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)发布于 2019-08-13 20:33:27
您不需要编写自己的JS代码,而是可能需要使用shinyWidgets包
具体来说,请看确认对话框:http://shinyapps.dreamrs.fr/shinyWidgets/
编辑:在这里你可以找到一些例子 .
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)https://stackoverflow.com/questions/57484835
复制相似问题