How to listen for more than one event expression within a Shiny eventReactive handler

RShiny

R Problem Overview


I want two different events to trigger an update of the data being used by various plots / outputs in my app. One is a button being clicked (input$spec_button) and the other is a point on a dot being clicked (mainplot.click$click).

Basically, I want to listed for both at the same time, but I'm not sure how to write the code. Here's what I have now:

in server.R:

data <- eventReactive({mainplot.click$click | input$spec_button}, {
    if(input$spec_button){
      # get data relevant to the button
    } else {
      # get data relevant to the point clicked
    }
  })

But the if-else clause doesn't work

  operations are possible only for numeric, logical or complex types```

 --> Is there some sort of action-combiner function I can use for the `mainplot.click$click | input$spec_button` clause?

R Solutions


Solution 1 - R

I know this is old, but I had the same question. I finally figured it out. You include an expression in braces and simply list the events / reactive objects. My (unsubstantiated) guess is that shiny simply performs the same reactive pointer analysis to this expression block as to a standard reactive block.

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

EDIT

Updated to handle the case where the last line in the expression returns NULL. Simply return a constant value.

Solution 2 - R

Also:

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

Solution 3 - R

I've solved this issue with creating a reactive object and use it in event change expression. As below:

xxchange <- reactive({
paste(input$filter , input$term)
})

output$mypotput <- eventReactive( xxchange(), {
...
...
...
} )

Solution 4 - R

Here's the solution I came up with: basically, create an empty reactiveValues data holder, and then modify its values based on two separate observeEvent instances.

  data <- reactiveValues()
  observeEvent(input$spec_button, {
    data$data <- get.focus.spec(input=input, premise=premise, 
                                itemname=input$dropdown.itemname, spec.info=spec.info)
  })
  observeEvent(mainplot.click$click, {
    data$data <- get.focus.spec(input=input, premise=premise, mainplot=mainplot(),
                                mainplot.click_focus=mainplot.click_focus(),
                                spec.info=spec.info)  
  })

Solution 5 - R

This can still be done with eventReactive by putting your actions in a vector.

eventReactive(
  c(input$spec_button, mainplot.click$click), 
{ ... } )

Solution 6 - R

The idea here is to create a reactive function which will execute the condition you want to pass in observeEvent and then you can pass this reactive function to check the validity of the statement. For instance:

validate_event <- reactive({
    # I have used OR condition here, you can use AND also
    req(input$spec_button) || req(mainplot.click$click)
})

observeEvent(validate_event(), 
    { ... } 
)

Keep Coding!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHillary SandersView Question on Stackoverflow
Solution 1 - RDuncan BrownView Answer on Stackoverflow
Solution 2 - RJustAnotherView Answer on Stackoverflow
Solution 3 - RSelcuk AkbasView Answer on Stackoverflow
Solution 4 - RHillary SandersView Answer on Stackoverflow
Solution 5 - RJD CaddellView Answer on Stackoverflow
Solution 6 - RVishal SharmaView Answer on Stackoverflow