The observeEvent and eventReactive functions

Suppose you want your app to react only under certain circumstances, such as when the user clicks a button. In these cases, you can use the observeEvent() or eventReactive functions. Both trigger when a specified event is fired. Let's work with our plot about the t-distribution:

library(shiny)#the UIui <- fluidPage(numericInput(inputId = "freedom",label =  "Degrees of Freedom:",                            value = 1, min = 1, max = 50, step = 1),                textInput(inputId = "title", label = "Chart title:",                          value = "prob. density distributions"),                actionButton(inputId = "upgrade", label = "Plot it!"),                plotOutput(outputId = "plot_a"))#the server functionserver <- function(input, output) {  up <- eventReactive(input$upgrade, { x <- seq(-5,5,.1) ...

Get Hands-On Data Science with R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.