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

Time:02-25

I have a shiny app where I want two different buttons to open the same Sweet Alert, I was doing this using observeEvent with req(input$1 | input$2). This worked until I moved the two buttons to their own individual sweetAlerts, now the observeEvent only works if both buttons pop up in the sweetAlert. See repex below:

Note: the goal of the reprex is to press either Alert 1 or Alert 2, then the button that pops up in the resulting sweetAlert to get a success.

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)

CodePudding user response:

Try this

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)

CodePudding user response:

I think you have to create a new actionButton and associated observeEvent() for each alert:

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)
  })
}
  • Related