How to listen to more than one event expression in a Shiny observEvent

I want two different events to trigger an observer. It has been suggested here that this should work. But it seems that this depends only on the second event.

observeEvent({ 
  input$spec_button
  mainplot.click$click
}, { ... } )

See an example.

ui <- shinyUI(bootstrapPage(
    actionButton("test1", "test1"),
    actionButton("test2", "test2"))
)

server <- shinyServer(function(input, output) {
    observeEvent({
        input$test1
        input$test2
    }, {
        print('Hello World')
    })
})

shinyApp(ui, server)

As soon as you press the test1 button, nothing will happen. If you press the test2 button, it prints on your console. After pressing the test2 button, pressing the test1 button prints a message. This is a weird behavior.

Another suggestion that was associated with was the use of

list(input$test1, input$test2)

That prints a message even without pressing buttons.

+4
source share
1 answer

, , , , @MrFlick

1.

#rm(list = ls())
library(shiny)
ui <- shinyUI(bootstrapPage(
  actionButton("test1", "test1"),
  actionButton("test2", "test2"))
)

server <- shinyServer(function(input, output) {

  toListen <- reactive({
    list(input$test1,input$test2)
  })
  observeEvent(toListen(), {
    if(input$test1==0 && input$test2==0){
      return()
    }
    print('Hello World')
  })
})

shinyApp(ui, server)

2. @MrFlick ( )

#rm(list = ls())
library(shiny)
ui <- shinyUI(bootstrapPage(
  actionButton("test1", "test1"),
  actionButton("test2", "test2"))
)

server <- shinyServer(function(input, output) {

  observeEvent(input$test1 | input$test2, {
    if(input$test1==0 && input$test2==0){
      return()
    }
    print('Hello World')
  })
})

shinyApp(ui, server)
+4

Source: https://habr.com/ru/post/1668412/


All Articles