KPI dashboard template for R Shiny

A Shiny dashboard with KPI value boxes, sidebar filters, a chart and a top-5 table, built with bslib on R’s airquality data. Free app.R to copy, edit and run.

The KPI dashboard template in the DropToApp editor: the app's layout with its inputs and outputs
The template in the DropToApp editor. Run it to see it working.

About this template

A classic dashboard layout: filters in a sidebar, three key numbers in value boxes across the top, and a chart and a table underneath. It uses the airquality dataset that comes with R (daily measurements in New York, May to September 1973), so it runs as is.

Every number updates as you change the filters. Swap in your own data in the setup code and the reactive filter in the server, and the layout keeps working.

What's inside

  • Value boxes whose values come from the server with textOutput()
  • A reactive() that filters the data once, shared by every output
  • A checkbox group and a slider as filters, with all months ticked to start
  • A full-screen card with a base R plot, and a table of the hottest days
  • The Flatly Bootswatch theme via bs_theme()

The complete app.R

Download
# Generated by DropToApp
library(shiny)
library(bslib)

# Daily air quality in New York, May to September 1973 (built into R)
aq <- airquality
aq$Date <- as.Date(paste(1973, aq$Month, aq$Day, sep = "-"))

# Average of x, rounded, or "-" when there is nothing to average
avg <- function(x, unit = "") {
  if (length(x) == 0 || all(is.na(x))) return("-")
  paste0(round(mean(x, na.rm = TRUE), 1), unit)
}

ui <- page_sidebar(
  title = "Air quality dashboard",
  sidebar = sidebar(
    title = "Filters",
    checkboxGroupInput(
      "months",
      "Months",
      choices = c("May", "June", "July", "August", "September"),
      selected = c("May", "June", "July", "August", "September")
    ),
    sliderInput(
      "min_temp",
      "Minimum temperature (F)",
      min = 56,
      max = 97,
      value = 56
    )
  ),
  theme = bs_theme(bootswatch = "flatly"),
  layout_columns(
    value_box(
      title = "Days",
      value = textOutput("n_days"),
      theme = "primary"
    ),
    value_box(
      title = "Average temperature",
      value = textOutput("avg_temp"),
      theme = "warning"
    ),
    value_box(
      title = "Average ozone (ppb)",
      value = textOutput("avg_ozone"),
      theme = "success"
    )
  ),
  card(
    full_screen = TRUE,
    card_header("Daily maximum temperature"),
    plotOutput("temp_plot", height = "300px")
  ),
  card(card_header("Hottest days"), tableOutput("hottest"))
)

server <- function(input, output, session) {
  # Inputs: input$months, input$min_temp

  filtered <- reactive({
    keep <- aq$Month %in% match(input$months, month.name) & aq$Temp >= input$min_temp
    aq[keep, ]
  })

  output$n_days <- renderText(nrow(filtered()))
  output$avg_temp <- renderText(avg(filtered()$Temp, " F"))
  output$avg_ozone <- renderText(avg(filtered()$Ozone))

  output$temp_plot <- renderPlot({
    d <- filtered()
    validate(need(nrow(d) > 0, "No days match the filters."))
    plot(d$Date, d$Temp, type = "h", lwd = 3, col = "#2c3e50",
         xlab = "", ylab = "Temperature (F)")
  })

  output$hottest <- renderTable({
    d <- filtered()
    d <- d[order(-d$Temp), c("Date", "Temp", "Ozone", "Wind")]
    d$Date <- format(d$Date, "%d %B")
    head(d, 5)
  })
}

shinyApp(ui, server)

How to run it

  1. In your browser: run it with shinylive. R runs on your computer with webR; the first start downloads R and the packages, which can take a minute.
  2. In R or RStudio: save the code as app.R, install the packages once with install.packages(c("shiny", "bslib")), then run shiny::runApp() in the folder with the file.
  3. In DropToApp: open the template in the editor to change the layout by drag and drop or edit the code, and export your own app.R.

More Shiny templates

Screenshot of the Data explorer Shiny template in the DropToApp editor

Data explorer

Pick a dataset and two columns to get a scatter plot, a searchable DT table and a summary, with axis choices that update per dataset. Free R Shiny template.

Data explorationDT tableTabs
Screenshot of the Feedback form Shiny template in the DropToApp editor

Feedback form

A feedback form in R Shiny: text, choice and rating inputs, a conditional field, validation, a live table of responses and a CSV download. Free app.R.

FormsValidationDownload
Screenshot of the Multi-page app Shiny template in the DropToApp editor

Multi-page app

A Shiny app with several pages in a navigation bar: an overview with value boxes and an accordion, an interactive plotly chart and a DT data table.

Multi-pagePlotlyAccordion
Screenshot of the CSV upload and explorer Shiny template in the DropToApp editor

CSV upload and explorer

Upload a CSV file, choose the separator, and see its rows, columns, a searchable table and a summary. Download the data again as CSV. Free Shiny app template.

File uploadDT tableDownload
Screenshot of the Histogram explorer Shiny template in the DropToApp editor

Histogram explorer

The classic first Shiny app, rebuilt with bslib: a slider and a variable picker drive a histogram of the Old Faithful geyser data. Free app.R to copy and run.

BeginnerCharts