Laura Biggins
10/12/2020
User changes value of the slider
This is flagged by Shiny
output$iris_plot is notified that it needs to re-run the code.
server <- function(input, output, session) {
output$iris_plot <- renderPlot({
point_size <- function(){
Sys.sleep(2)
input$slider + 0.1
}
iris %>%
ggplot(aes(x = .data[[input$x_attribute]],
y = .data[[input$y_attribute]])) +
geom_point(size = point_size(),
colour = input$radio_colour) +
ggtitle(input$plot_name)
})
Reactive expressions can access reactive values or other reactive expressions and they return a value. They can cache values.
server <- function(input, output, session) {
point_size <- reactive({
Sys.sleep(2)
input$slider + 0.1
})
output$iris_plot <- renderPlot({
iris %>%
ggplot(aes(x = .data[[input$x_attribute]],
y = .data[[input$y_attribute]])) +
geom_point(size = point_size(),
colour = input$radio_colour) +
ggtitle(input$plot_name)
})
Reactive expressions can be called by render functions. Render functions are a special implementation of an observer. They are eager, not lazy and so will re-evaluate the code when a reactive value or expression changes.
There are other observers that aren’t render functions, observeEvent() and observe().
The value of input$text_field can still be accessed but any changes in text_field will not cause the code to be re-evaluated.
Call a reactive expression as you would a function
For each exercise open the app.R file and follow the instructions set out in the comments at the top of the script.
Using a reactive expression
Using reactive() and isolate()
Both store expressions that can be executed.
Reactive expresssions need an observer as a descendent in order to execute.
reactive() – calculating values without side effects
observe() – performing actions with side effects
Examples of lazy vs eager, reactlog, observeEvent(), eventReactive()
observeEvent()
Code will evaluate when reactive values in first argument change, i.e. only when input$update changes.
reactive()
Reactive expression
reactiveVal()
Single reactive value that can be set and updated
x_values <- -20:20
server <- function(input, output) {
line_width <- reactiveVal(3)
y_values <- reactiveVal(x_values)
observeEvent(input$lwd_increase, {
line_width(line_width()*1.5)
})
observeEvent(input$lwd_decrease, {
line_width(line_width()/1.5)
})
observeEvent(input$plot_x_squared, {
y_values(x_values^2)
})
observeEvent(input$plot_x_cubed, {
y_values(x_values^3)
})
output$x_plot <- renderPlot({
plot(x_values,
y_values(),
type = "l",
lwd = line_width())
})
}
x_values <- -20:20
server <- function(input, output) {
rv <- reactiveValues(
line_width = 3,
y_values = x_values^2
)
observeEvent(input$lwd_increase, {
rv$line_width <- rv$line_width*1.5
})
observeEvent(input$lwd_decrease, {
rv$line_width <- rv$line_width/1.5
})
observeEvent(input$plot_x_squared, {
rv$y_values <- x_values^2
})
observeEvent(input$plot_x_cubed, {
rv$y_values <- x_values^3
})
output$x_plot <- renderPlot({
plot(x_values,
rv$y_values,
type = "l",
lwd = rv$line_width)
})
}
reactive()
Reactive expression
eventReactive()
Reactive expression that is only evaluated when reactive value in first argument is invalidated.
reactiveVal()
Single reactive value that can be set and updated
observeEvent()
observe()
Both store expressions that can be executed.
Reactive expresssions need an observer as a descendent in order to execute.
reactive() – calculating values without side effects
observe() – performing actions with side effects
UI inputs may need to be updated after user actions. There are a set of update functions available.
update[inputType]
updateSelectInput(session, id, choices = c("x", "y", "z"))
updateSliderInput(session, id, min = 10, max = 50, step = 5)
https://shiny.rstudio.com/articles/understanding-reactivity.html
https://mastering-shiny.org/ early online version of book by Hadley Wickham
https://resources.rstudio.com/shiny-developer-conference/shinydevcon-reactivity-joecheng-part-1-1080p Talk by Joe Cheng about reactivity