I read the Shiny documentation about reactive programming a few times now, but I can't properly understand the difference between observeEvent
and eventReactive
.
The documentation says:
Use observeEvent whenever you want to perform an action in response to an event. (Note that "recalculate a value" does not generally count as performing an action–see eventReactive for that.)
....
Use eventReactive to create a calculated value that only updates in response to an event. This is just like a normal reactive expression except it ignores all the usual invalidations that come from its reactive dependencies;
In all the situation I tried I saw no difference between using observeEvent
and eventReactive
(the code works just fine regardless the function I use, with no apparent impact in performances).
Can you help me figure out what is the real difference between the two? Ideally I would like a few examples showing when they are interchangeable, one when observeEvent
would work but not eventReactive
and vice versa.
Use observeEvent whenever you want to perform an action in response to an event. (Note that "recalculate a value" does not generally count as performing an action--see eventReactive for that.)
observe and observeEvent are similar to reactive expressions. The big difference is that the observers do not yield any output and thus they are only useful for their side effects.
eventReactives are similar to reactives, they are constructed as follows: eventReactive( event { code to run }) eventReactives are not dependent on all reactive expressions in their body ('code to run' in the snippet above). Instead, they are only dependent on the expressions specified in the event section.
The isolate function lets you read a reactive value or expression without establishing this relationship. The expression given to isolate() is evaluated in the calling environment. This means that if you assign a variable inside the isolate() , its value will be visible outside of the isolate() .
As @daatali is saying the two functions are used for different purposes.
ui <- shinyUI(pageWithSidebar( headerPanel("eventReactive and observeEvent"), sidebarPanel( actionButton("evReactiveButton", "eventReactive"), br(), actionButton("obsEventButton", "observeEvent"), br(), actionButton("evReactiveButton2", "eventReactive2") ), mainPanel( verbatimTextOutput("eText"), verbatimTextOutput("oText") ) )) server <- shinyServer(function(input, output) { etext <- eventReactive(input$evReactiveButton, { runif(1) }) observeEvent(input$obsEventButton,{ output$oText <- renderText({ runif(1) }) }) eventReactive(input$evReactiveButton2,{ print("Will not print") output$oText <- renderText({ runif(1) }) }) output$eText <- renderText({ etext() }) }) shinyApp(ui=ui,server=server)
eventReactive
creates a reactive value that changes based on the eventExpr
while observeEvent
simply is triggered based on eventExpr
It's like the difference between observe
and reactive
. One is intended to be run when some reactive variable is "triggered" and is meant to have side effects (observeEvent
), and the other returns a reactive value and is meant to be used as a variable (eventReactive
). Even in the documentation for those functions, the former is shown without being assigned to a variable (because it is intended to just produce a side effect), and the latter is shown to be assigned into a variable and used later on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With