Shiny 中嵌套 sweet_alert 中的按钮中断

Button breaks in nested sweet_alert in Shiny

在这个应用程序中,我有两个嵌套的 sweet_alert 弹出窗口,类似于 Shiny 的 modalDialogs。

第一个警报中有一个按钮可以激活第二个警报。然后弹出第二个警报取代第一个警报。

第一轮一切正常。之后,第二个按钮停止工作。

要遵循的步骤:

  1. 单击按钮 1 -> 查看警报 1
  2. 单击按钮 2 -> 警报 1 替换为警报 2
  3. 按确定,关闭 alert2
  4. 单击按钮 1 -> 查看警报 1
  5. 单击 Button2 -> 没有任何反应

为什么 Button2 在第一轮后就坏了?以及如何修复?

library('shinyWidgets')
ui = fluidPage(
    actionButton("btn1", "Button1")
)

server = function(input, output, session) {

  observeEvent(input$btn1, {
        show_alert(
        title = "This is the first popup!",
        text = tags$div(
            actionButton("btn2", "Button2")
        ),
        html = TRUE,
        width = "80%"
        )
    })

    observeEvent(input$btn2, {
        show_alert(
        title = "This is the second popup!",
        html = TRUE,
        width = "80%"
        )
    })
}
runApp(shinyApp(ui, server))

解决这个问题的一种方法是使用 closeSweetAlert() 重置 btn2。试试这个

library('shinyWidgets')
ui = fluidPage(
  actionButton("btn1", "Button1")
  #,verbatimTextOutput("t1")
)

server = function(input, output, session) {
  
  observeEvent(input$btn1, {
    show_alert(
      title = "This is the first popup!",
      text = tags$div(
        actionButton("btn2", "Button2")
      ),
      html = TRUE,
      width = "80%"
    )
  })
  
  output$t1 <- renderPrint(input$btn2)
  
  observeEvent(input$btn2, {
    closeSweetAlert(session = shiny::getDefaultReactiveDomain())
    show_alert(
      title = "This is the second popup!",
      html = TRUE,
      width = "80%"
    )
  })
}
runApp(shinyApp(ui, server))