← Module 12 overview
Module 12 · Project 6 · Due the date on Canvas · Interactive Dashboards with Shiny · adaptive competency DV20

Wire the reactive graph

Module 12 · Step 2 of 9 · Guided8–10 min

Why this lab Wire the reactive graph so one input change updates only what depends on it.

An input change invalidates dependent reactive expressions and outputs. Reactive expressions recompute when requested and cache their results; independent outputs have no guaranteed execution order. Shiny follows the reactive programming model, in which the runtime records the dependencies among time-varying values and updates dependent values automatically when their sources change (Bainomugisha et al., 2013). Build the dependency path in order: user input, reactive data, verified summary, rendered output.

Input→Reactive expression→Summary→Output
Before the lab · how a reactive expression works
  1. Read inputs inside a reactive context. A line such as selected <- input$campus written outside the server's reactive code cannot read the input. Wrapping it as reactive({ input$campus }) records the dependency, so a campus change invalidates it; Shiny recomputes its value when a consumer requests it. isolate() does the opposite: it reads a value without recording a dependency.
  2. Call a reactive expression to get its value. filtered_data <- reactive({ ... }) creates something that behaves like a function. To retrieve the current filtered data, call it with parentheses: filtered_data(), inside a render function or another reactive. Written without the parentheses, filtered_data is the expression object, not the data.
  3. Only what depends on the change is rerun. One shared reactive expression, called by every output that needs it, keeps the filter logic in one place, and a change of input invalidates exactly the outputs that depend on it.
Extend this (optional): when a reactive value is recomputed

The dependencies form a directed acyclic graph whose sources are inputs and whose sinks are outputs. Reactive languages differ in how a change travels through that graph: push-based propagation recomputes dependents as soon as a source changes, whereas pull-based propagation recomputes a value only when something demands it. A language can also show a glitch, a moment in which a dependent value has been computed from a mixture of old and new sources (Bainomugisha et al., 2013). Shiny combines the two directions: an input change pushes invalidation forward through the graph, and a reactive expression is recomputed only when an output pulls its value. Because Shiny marks every affected value invalid before it recomputes any of them, an output that pulls a reactive expression does not combine a stale intermediate value with a new input.

Reactive graph builder loading…