【问题标题】:Override dismiss button in R Shiny modal dialogue覆盖 R Shiny 模态对话框中的关闭按钮
【发布时间】:2019-08-14 04:42:47
【问题描述】:

我在 R Shiny 中使用模态对话来获取用户的输入。在此表单中,默认情况下有一个关闭按钮,单击该按钮会关闭该表单。我想在单击关闭按钮时添加一个确认弹出窗口(sweetAlert)。

我也准备好使用 javascript,但我需要 sweetAlert 而不是 windows 警报。我也无法成功生成 Windows 警报。

如何覆盖此内置“关闭”按钮的功能?我想在有人点击解雇时显示警告,并且只有在他们确定时才让他们继续。否则我想让他们留在模态对话中。

感谢任何帮助。

【问题讨论】:

    标签: javascript r shiny modal-dialog sweetalert


    【解决方案1】:

    这是一种方法。代码相当简单。 -

    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)
    

    【讨论】:

    • 这正是我想要的。页脚参数对我有用。它会覆盖默认的关闭按钮。非常感谢您的正确和快速的回​​答:)
    【解决方案2】:

    您不需要编写自己的 JS 代码,而是可能想使用 shinyWidgets package

    具体来说,看一下确认对话框: http://shinyapps.dreamrs.fr/shinyWidgets/

    编辑Here you can find some examples,例如

    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)
    

    【讨论】:

      猜你喜欢
      • 2016-12-10
      • 2023-01-30
      • 2013-11-10
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 2012-12-17
      相关资源
      最近更新 更多