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

Hillary Sanders picture Hillary Sanders · Jan 11, 2016 · Viewed 34.1k times · Source

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

Error in mainplot.click$click | input$spec_button : 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?

Answer

Duncan Brown picture Duncan Brown · Oct 21, 2016

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.