Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

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?

like image 283
Hillary Sanders Avatar asked Jan 11 '16 22:01

Hillary Sanders


3 Answers

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.

like image 141
Duncan Brown Avatar answered Oct 25 '22 15:10

Duncan Brown


Also:

observeEvent(c( 
  input$spec_button,
  mainplot.click$click
), { ... } )
like image 29
JustAnother Avatar answered Oct 25 '22 15:10

JustAnother


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(), {
...
...
...
} )
like image 40
Selcuk Akbas Avatar answered Oct 25 '22 15:10

Selcuk Akbas