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 UI
ui <- 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 function
server <- function(input, output) {
up <- eventReactive(input$upgrade, {
x <- seq(-5,5,.1)
par(lwd = 2)
plot(x, dnorm(x), type = 'l', ylab = 'density',
main = input$title)
lines(x, dt(x, input$freedom), col = '#e66101', lty = 2)#input$freedom
legend('topright', legend = c('normal','t-student'),
col = c('#000000','#e66101'), lty = 1:2)})
to_print <- observeEvent(input$upgrade, {print(list(input$title,
input$freedom))})
output$plot_a <- renderPlot(up())
}
#Run the app
shinyApp (ui = ui, server = server)

A couple of things were changed in our app since the last time we saw it. We added a text input, an action button in the user's interface, and we used some new functions inside the server function. Once you run the code, at the first moment, there is no plot in the app. Click on Plot it! and it will appear with the number of degrees of freedom chosen in the numeric input and with the text input as the plot title. The plot event was delayed by the use of the eventReactive function, but how did we use it?

Calling the eventReactive function requires two elements: the trigger input and the expression to be executed when the trigger is fired. In our case, the first element is our button, or how we locate it: input$upgrade; the second element is all of the code necessary to build our previous plot. Note that we used the input$freedom and input$title tags on it, right in the places where we want the users to interact with the degrees of freedom from the t-distribution and with the chart title, respectively. So the eventReactive(,{}) function was used with two elements. Before the button triggers, the second element inside the eventReactive function is treated as isolated; when you click on the button, the input$upgrade input value reacts, calling the second element, or the code to build our plot. Where will the plot be placed?

To set the plot in the right place, we called the eventReactive function in a new element, creating the up() reactive expression. So, the last thing we need to do is call the up() expression—remember to put the parentheses inside the proper render function in the output object designated for it, in our case, the output$plot_a element.

If you paid close attention when you clicked on the button Plot it!, the Shiny app, apart from inserting the plot in the web page, also prints the degree of freedom and the title chosen by the user in your R console, but this operation is not shown in the UI. The observeEvent(,{}) function is responsible for it. The syntax is the same as the eventReactive function, so when its first element is triggered, the expression inside the second element is executed, but it is not shown to the users. So, basically, the observeEvent function creates a reactive expression to be triggered under a specific situation; when this situation happens, it forces the server to run the code expression in a second plan, not showing any results to the user. It is useful if you want to hide some executions from your users. The observer() function can be used similarly, but it uses the same syntax as the reactive() function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.156.107