33  Advanced Interactive Visualization and Dashboards

33.1 Learning objectives

By the end of this chapter you should be able to:

  • Use plotly and htmlwidgets to convert static visualizations into interactive web graphics, and decide when interactivity adds value versus when it adds distraction.
  • Architect a shiny application that scales beyond a single user and a single session, using bslib, shinyloadtest, and reactive-graph discipline.
  • Build dashboards with flexdashboard, Quarto dashboards, and shiny dashboards built with bslib::page_navbar() or bs4Dash, and choose between them based on the audience and update cadence.
  • Use Observable JS cells and ojs_define() in Quarto to embed reactive analytics in static documents.
  • Apply principles of effective dashboard design (hierarchy of information, calibrated color, semantic accessibility) to an analytics dashboard for a clinical audience.

33.2 Orientation

A static plot answers a question. A dashboard, by contrast, mediates a conversation. The user asks something the designer anticipated, the dashboard responds, and the user asks the follow-up. This shift from artifact to conversation changes both the engineering and the design.

In this chapter we shall be concerned mainly with the engineering. The design half, that is, how to compose a dashboard that supports decision-making rather than confusing the user, receives one section here. It is the subject of book-length treatments (Few, 2013; Munzner, 2014). Our focus is the toolkit: plotly for interactive graphics, shiny for reactive applications, dashboard frameworks for at-a-glance displays, and Observable JS for in-document reactivity.

We note at the outset that most analytic questions are better answered by a well-designed static figure than by a dashboard. Static output is therefore the default throughout this chapter, and interactivity has to justify itself case by case.

We begin with the judgments the statistician contributes (Section 33.3). We then treat interactive plots with plotly and htmlwidgets, reactive applications with shiny, the dashboard frameworks, and Observable JS in turn. A worked clinical quality dashboard and a section on collaborating with an LLM complete the chapter.

33.3 The statistician’s contribution

LLMs can write shiny code, configure plotly tooltips, and lay out dashboards. Unfortunately, they cannot decide which interactions a clinician actually wants or which default views inform decisions. Nor can they judge whether the dashboard is delivering information or merely flattering the user with graphics.

(Judgment 1.) Interactivity that does not help is distraction. Users routinely ask for filters and toggles that they will not use. Building them costs engineering time, complicates testing, slows page load, and obscures the data behind layers of UI. The statistician’s role is to push back on requested interactions until the user can articulate what decision the interaction informs. ‘I want to filter by year’ is not yet a decision. ‘I want to see this year’s quality measure trends to decide where to intervene’ is one.

(Judgment 2.) Defaults are the dashboard. Most users look at a dashboard’s default state and never change it. The default view, default filters, default time range, and default sort order are the dashboard for those users. Building elaborate alternative views that no one will discover is wasted work. The statistician decides what the default state should communicate, and the secondary views are optional refinements.

(Judgment 3.) Calibration applies to dashboards. Numbers reported on dashboards are taken at face value. A dashboard that displays predicted readmission risk to clinicians is making a calibration claim by displaying the number. If the underlying model is poorly calibrated, the dashboard is misleading. The plea that ‘we just display the model output’ is not a defense. The statistician is responsible for the accuracy of every quantity the dashboard shows, including the ones the user did not ask about.

33.4 Interactive plots: plotly and htmlwidgets

The shortest path from a ggplot2 figure to an interactive version is plotly::ggplotly():

library(ggplot2)
library(plotly)

p <- ggplot(d, aes(x = age, y = los, colour = service)) +
  geom_point(alpha = 0.4) +
  facet_wrap(~ quarter)

ggplotly(p)

ggplotly() translates the ggplot to a Plotly graph, preserving most aesthetics and adding hover tooltips, zoom, pan, and a legend toggle. For most exploratory work this is enough.

For dashboard or report contexts that need more control (custom hover content, click events, animation), Plotly’s native syntax via plot_ly() is preferable. The native syntax is also faster and produces smaller HTML output for large data.

Other htmlwidgets-based packages cover specialized needs:

  • leaflet for interactive maps.
  • DT for searchable, sortable, paginated tables: the de facto standard for data tables in dashboards.
  • reactable for tables with conditional formatting and inline interactivity, more flexible than DT for custom layouts.
  • networkD3 / visNetwork for graph and network visualization.
  • echarts4r for richer chart types than plotly (gauges, sankey, treemaps).

We note one practical performance issue. By default, htmlwidgets embed all data in the rendered HTML. A dashboard with 100,000 points displayed in plotly therefore produces a large HTML file and can be slow to render. For large data we therefore reduce the data before rendering, by subsampling, aggregating, or filtering. In a shiny app this reduction can run on the server, inside plotly::renderPlotly(), so that only the reduced data are sent to the browser. We note that renderPlotly() alone does not shrink the payload, because the widget still carries whatever data it is given.

33.5 Reactive applications with shiny

shiny (Chang et al., 2024) is the workhorse for serious analytic applications in R. The architecture is reactive: the UI declares inputs and outputs, the server defines how outputs depend on inputs, and shiny wires them together.

To illustrate, consider a minimal application:

library(shiny)
library(bslib)

ui <- page_sidebar(
  title = "Cohort explorer",
  sidebar = sidebar(
    selectInput("service", "Service",
                choices = c("med", "surg", "cards")),
    sliderInput("year", "Year", 2018, 2024,
                value = c(2022, 2024), sep = "")
  ),
  card(card_header("Outcomes by service"),
       plotOutput("plot")),
  card(card_header("Cohort summary"),
       tableOutput("summary"))
)

server <- function(input, output, session) {
  filtered <- reactive({
    d |>
      filter(service == input$service,
             year >= input$year[1],
             year <= input$year[2])
  })
  output$plot <- renderPlot({
    ggplot(filtered(), aes(x = los, y = readmit)) +
      geom_smooth(method = "lm") + geom_point()
  })
  output$summary <- renderTable({
    filtered() |>
      summarise(n = n(), mean_los = mean(los),
                readmit_rate = mean(readmit == "yes"))
  })
}

shinyApp(ui, server)

bslib (Aden-Buie et al., 2024) is the modern UI framework for shiny. It replaces the older shinydashboard look with a Bootstrap 5 design system that supports theming, responsive layouts, and dark mode. Most new applications should start with bslib.

33.5.1 Reactive-graph discipline

The most common cause of slow shiny applications is unintended re-execution. A reactive({ ... }) re-runs when any input it reads changes and caches the result for all its readers. A renderPlot({ ... }) likewise re-runs when any reactive it reads changes. Without discipline, a single input change can trigger a cascade of re-renders.

We offer three rules.

  1. Compute once, use many times. If two outputs depend on the same filtered data, compute the filter in a reactive() and read it from both. Do not re-filter in each renderX().

  2. Defer expensive operations. bindCache() caches reactive computations across sessions; bindEvent() and eventReactive() decouple expensive computations from input changes that should not trigger them.

  3. Respect the req() boundary. A reactive that uses req(input$x) halts the reactive chain if input$x is NULL. This prevents unnecessary computation during initial render.

An application with many users must also scale. shinyloadtest (Dipert, 2024) simulates concurrent sessions and measures response times. The results often reveal that a single-server application supports 10 to 20 concurrent users before degrading. Scaling beyond that requires either horizontal scaling (multiple R processes behind a load balancer, via shinyapps.io or Posit Connect) or aggressive caching of the expensive operations.

Question. A collaborator asks for a dashboard displaying weekly clinical-quality metrics. The data update once a week, and all users see the same view. No filtering or interaction is needed beyond viewing the plots. Should this be a shiny application?

Answer.

No. The use case is a static report, not a reactive application. A Quarto document parameterized by week, rendered to HTML and republished weekly via cron or GitHub Actions, is the right tool. It is simpler, cheaper to host, faster to load, and accessible without server infrastructure. shiny adds value only when the user interacts. If the only ‘interaction’ is viewing a fixed display, the static report is the better choice.

33.6 Dashboard frameworks

Three dashboard frameworks dominate in 2026:

Quarto dashboards (Posit Software, PBC, 2024), added in Quarto 1.4, let you author a multi-pane dashboard in Markdown with code chunks. They render to a static HTML file with no server required. They are the right tool when the data update on a schedule and the dashboard does not need user-driven filtering.

flexdashboard is the older, still-supported predecessor with a similar mental model and slightly different layout primitives. New work should prefer Quarto dashboards.

shiny dashboards via bslib::page_navbar() or bs4Dash are the choice when reactive interactivity is required. On the minus side, they are heavier and need a server. On the plus side, they support the full reactive programming model.

The choice may be summarized as a decision table.

Need Tool
Static, scheduled refresh Quarto dashboard
Reactive filtering, single user shiny + bslib
Reactive, many concurrent users shiny + caching + horizontal scaling
Embedded reactive in a report Observable JS in Quarto

33.7 Observable JS in Quarto documents

Observable JS (OJS) is JavaScript-based reactive notebook syntax adapted for Quarto. It provides reactive interactivity without a server, because the computation runs in the user’s browser. The trade-off is twofold. OJS reactives must be written in JavaScript (or in R/Python with data passed via ojs_define()). The data, moreover, must be small enough to ship in the rendered HTML.

The basic pattern is as follows.

```{r}
ojs_define(d = readmissions)
```

```{ojs}
viewof service = Inputs.select(
  ["med", "surg", "cards"],
  {label: "Service"}
)

filtered = d.filter(r => r.service == service)

Plot.plot({
  marks: [
    Plot.dot(filtered, {x: "los", y: "readmit_prob"})
  ]
})
```

(The double-braced {r} and {ojs} are Quarto’s escape syntax for showing chunk-syntax literally inside another code block. In an actual document, single braces are written.)

OJS is the right tool when:

  • The data is small enough to embed (typically under a few MB).
  • The reactivity is genuinely client-side (filter, sort, summarize, plot) and does not require server computation.
  • The deliverable is a self-contained HTML file that works without a server.

For large datasets, server computation, or full reactive applications, shiny remains the answer.

33.8 Worked example: a clinical quality dashboard

To illustrate, consider a monthly-refreshed dashboard that displays readmission rates, length of stay, and patient satisfaction by service line. It is built as a Quarto dashboard, and the data refresh on the first of each month via a cron-driven render.

---
title: "Quality metrics: April 2026"
format: dashboard
---

```{r}
#| include: false
library(tidyverse); library(plotly); library(DT)
d <- readRDS("data/quality_metrics_apr2026.rds")
```

# Overview

## Row {height=20%}

```{r}
#| content: valuebox
list(
  icon = "hospital",
  color = "primary",
  value = nrow(d),
  title = "Discharges (month)"
)
```

```{r}
#| content: valuebox
list(
  icon = "arrow-repeat",
  color = if (mean(d$readmit) > 0.12) "danger"
          else "success",
  value = sprintf("%.1f%%", 100 * mean(d$readmit)),
  title = "Readmission rate"
)
```

## Row {height=80%}

### Column

```{r}
p <- d |>
  group_by(service, month) |>
  summarize(rate = mean(readmit), .groups = "drop") |>
  ggplot(aes(month, rate, color = service)) +
  geom_line() + geom_point() +
  labs(y = "Readmission rate", x = NULL)
ggplotly(p)
```

### Column

```{r}
d |>
  group_by(service) |>
  summarize(n = n(), mean_los = mean(los),
            readmit = mean(readmit)) |>
  datatable(options = list(dom = "t"),
            rownames = FALSE)
```

# Data quality

## Row

(...further pages with data-quality checks)

The valuebox cells communicate the headline numbers, which clinicians scan first. The line chart then provides trend context, and the data table at the right provides drill-down detail. The conditional color on the readmission valuebox (red above 12%, green below) is a deliberate decision. That is, the dashboard communicates a judgment about the value along with the value itself. Such a judgment should be reviewed by clinical stakeholders rather than invented by the analyst.

The dashboard renders to a self-contained HTML file. A GitHub Actions workflow runs quarto render against fresh data on the first of every month and publishes the result. No shiny server is needed, and the cost is near zero. The output is fast to load and straightforward to share.

33.9 Collaborating with an LLM on interactive visualization

LLMs handle shiny and Quarto dashboard syntax fluently. They are weaker on dashboard design judgment and on performance reasoning.

Prompt 1: ‘Convert this ggplot to plotly with hover tooltips showing patient ID and discharge date.’

What to watch for. The LLM will produce working code quickly. It may default to ggplotly() even when plot_ly() would render faster on the data size. It may also embed sensitive identifiers (patient ID) in the rendered HTML. If the dashboard is shared, this is a privacy violation.

Verification. We inspect the rendered HTML for any identifying information that should not be shared. We also check rendering time on representative data. If rendering is slow, we switch to plot_ly() or aggregate the data before plotting.

Prompt 2: ‘Add a filter for service line and a date range to this shiny app.’

What to watch for. The LLM will produce working reactive code. However, it may not factor the filter into a single reactive(), which leads to redundant computation. It may also omit req(input$service) for the initial state. It will rarely test the application under any meaningful load.

Verification. We read the resulting reactive graph in reactlog and confirm that the filters compute once rather than in each renderX(). We then run shinyloadtest at a target concurrency level before deploying to a multi-user environment.

Prompt 3: ‘Design a dashboard for clinical leadership to monitor readmissions.’

What to watch for. The LLM will produce a layout, typically with too many panels and too many chart types. It will not ask what decisions clinical leadership makes or what view should be the default. Instead, it tends to populate a dashboard with everything available rather than with the few quantities that matter.

Verification. Before implementing, we articulate the two or three decisions the dashboard supports and build for those, treating additional panels as optional. A dashboard that fits on a single screen with three carefully chosen panels is almost always more useful than the eight-panel sprawl the LLM produces.

The meta-pattern is that LLMs accelerate the building of UI but do not improve its design. We therefore treat the LLM as fast hands, and we bring our own taste and our own contact with the end user.

33.10 Principle in use

Three habits define defensible interactive-visualization work:

  1. Default to static. A static figure or a static parameterized report should be the working assumption. Interactivity is a feature you add when you can name the decision it informs.

  2. Engineer for the default state. Most users look at the dashboard we ship without changing anything. The default view, default filters, and default sort are the dashboard. Make them informative.

  3. Test under load before deploying. A shiny application that works on a laptop may collapse under five concurrent users. We run shinyloadtest before deploying to a multi-user setting, and we cache and scale the bottlenecks before users find them.

33.11 Exercises

  1. Take a static ggplot2 visualization from a previous chapter. Convert it to plotly, then to OJS. Compare the file sizes, load times, and user experience for a colleague who has not seen the figure before.

  2. Build a shiny application with a single filter and two dependent outputs. Implement it once with the filter inside both renderX calls and once with the filter in a shared reactive(). Use reactlog to confirm the difference in re-execution.

  3. Convert a quarterly report you produce as a static document to a Quarto dashboard. Identify which elements gain from being on a dashboard and which lose from the constraint of a single screen.

  4. Run shinyloadtest against a shiny application of your choice with simulated concurrency of 10, 50, and

    1. Document the response-time degradation. Implement bindCache() on the slowest reactive and re-test.
  5. Critique a clinical or epidemiological dashboard you have seen in production. Identify three design choices that aid decision-making and three that obscure it. Propose specific changes.

33.12 Further reading