当元素不在标准 ui 页面中时,R Shiny req() with or 语句不起作用

R Shiny req() with or statement not working when elements aren't in a standard ui page

我有一个闪亮的应用程序,我想要两个不同的按钮来打开同一个 Sweet Alert,我使用带有 req(input$1 | input$2) 的 observeEvent 来做到这一点。这一直有效,直到我将两个按钮移动到它们自己的单独的 sweetAlerts,现在只有当两个按钮都在 sweetAlert 中弹出时,observeEvent 才有效。请参阅下面的 repex:

注意:reprex 的目标是按下 A​​lert 1A​​lert 2,然后按下结果 sweetAlert 获得成功。

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
  
    mainPanel(
      actionButton('alert1','Alert 1'),
      actionButton('alert2','Alert 2')
    )
)

server <- function(input, output,session) {
  
  observeEvent(input$alert1,{
    sendSweetAlert(session=session,title = "Alert 1",text=tags$div(
      actionButton('alert1New', 'Show New Alert')
    ),type="info",btn_labels = NA, showCloseButton = T)
  })
  
  observeEvent(input$alert2,{
    sendSweetAlert(session=session,title = "Alert 2",text=tags$div(
      actionButton('alert2New', 'Show New Alert')
    ),type="info",btn_labels = NA, showCloseButton = T)
  })
  
  observeEvent(req(input$alert1New | input$alert2New),{
    sendSweetAlert(session=session,title = "Success",type="success", showCloseButton = T)
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

试试这个

library(shiny)
library(shinyWidgets)
ui <- fluidPage(
  
  mainPanel(
    actionButton('alert1','Alert 1'),
    actionButton('alert2','Alert 2')
  )
)

server <- function(input, output,session) {
  rv <- reactiveValues(cntr=0)
  
  observeEvent(input$alert1,{
    rv$cntr <- rv$cntr+1
    sendSweetAlert(session=session,title = "Alert 1",text=tags$div(
      actionButton(paste0('alert1New',rv$cntr), 'Show New Alert')
    ),type="info",btn_labels = NA, showCloseButton = T)
  })
  
  observeEvent(input$alert2,{
    rv$cntr <- rv$cntr+1
    sendSweetAlert(session=session,title = "Alert 2",text=tags$div(
      actionButton(paste0('alert2New',rv$cntr), 'Show New Alert')
    ),type="info",btn_labels = NA, showCloseButton = T)
  })
  
  observeEvent(c(input[[paste0('alert1New',rv$cntr)]], input[[paste0('alert2New',rv$cntr)]]),{
    sendSweetAlert(session=session,title = "Success",type="success", showCloseButton = T)
  })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

我认为您必须为每个警报创建一个新的 actionButton 和关联的 observeEvent()

server <- function(input, output,session) {
  rv <- reactiveValues(cntr=0, event=0)
  
  observeEvent(input$alert1,{
    rv$cntr <- rv$cntr+1
    observeEvent(input[[paste0('alert1New',rv$cntr)]],{rv$event = rv$cntr})
    sendSweetAlert(session=session,title = "Alert 1",text=tags$div(
      actionButton(paste0('alert1New',rv$cntr), 'Show New Alert')
    ),type="info",btn_labels = NA, showCloseButton = T)
  })
  
  observeEvent(input$alert2,{
    rv$cntr <- rv$cntr+1
    observeEvent(req(input[[paste0('alert2New',rv$cntr)]]),{rv$event = rv$cntr})
    sendSweetAlert(session=session,title = "Alert 2",text=tags$div(
      actionButton(paste0('alert2New',rv$cntr), 'Show New Alert')
    ),type="info",btn_labels = NA, showCloseButton = T)
  })
  
  observeEvent(req(rv$event>0), {
     sendSweetAlert(session=session,title = "Success",type="success", showCloseButton = T)
  })
}