Shiny: what is the difference between observeEvent and eventReactive?
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.