Skip to contents

Introduction

This vignette focuses on building Crunch dashboards with two helper functions:

The typical flow is:

  1. Build a crunch_dashboard_spec tibble with folder and tile_code.
  2. Generate the automation script with crunch_generate_dashboard().
  3. Run the script against the dataset with runCrunchAutomation().

How crunch_generate_tile() works

crunch_generate_tile() returns a single character string containing the Crunch dashboard DSL for one variable. The output depends on the variable type:

  • categorical and multiple_response generate a bar chart frequency tile.
  • categorical_array generates a card with subvariable totals.
  • numeric generates a histogram.
  • text generates a table of counts.

If group_by is provided and the type supports crosstabs (categorical, multiple_response, text), the tile switches from FREQUENCY to CROSSTAB and splits results by the grouping variable.

The weight_var argument adds a WEIGHTED BY clause and a footnote. If weight_var is NULL, the tile is unweighted and the footnote states that.

Examples

Default usage

tile_gender <- crunch_generate_tile(
  alias = "s_gender",
  name = "Gender",
  type = "categorical",
  description = "Self-reported gender"
)

tile_gender

Using weights

tile_gender_with_weight <- crunch_generate_tile(
  alias = "s_gender",
  name = "Gender",
  type = "categorical",
  description = "Self-reported gender (weighted)",
  weight_var = "weight"
)

tile_gender_with_weight

Grouped-by crosstab

tile_gender_grouped_by <- crunch_generate_tile(
  alias = "s_gender",
  name = "Gender",
  type = "categorical",
  description = "Self-reported gender by Country",
  group_by = "s_country"
)

tile_gender_grouped_by

Other options

tile_gender_grouped_by <- crunch_generate_tile(
  alias = "s_gender",
  name = "Gender",
  type = "categorical",
  description = "Self-reported gender",
  palette = "CZI",
  axis_position = "LEFT",
  tile_width = 12,
  tile_height = 10
)

tile_gender_grouped_by

Build the dashboard spec

crunch_generate_dashboard() expects a tibble with two columns:

  • folder: Folder path used to build tabs and sections. Use / to create nested sections.
  • tile_code: DSL produced by crunch_generate_tile().
crunch_ds <- crunch_connect()
crunch_dictionary <- crunch_get_dictionary(crunch_ds)

crunch_dashboard_spec <- crunch_dictionary |>
  dplyr::filter(!is.na(.data$folder)) |>
  dplyr::rowwise() |>
  dplyr::mutate(
    tile_code = as.character(crunch_generate_tile(
      .data$alias,
      .data$name,
      .data$type,
      .data$description
    ))
  ) |>
  dplyr::ungroup() |>
  dplyr::select("folder", "tile_code")

Generate and run the dashboard

crunch_generate_dashboard() writes a Crunch automation script to crunch/{name}. It also creates an Overview tab, so folder values should not use Overview as the top-level folder name.

crunch_generate_dashboard(
  crunch_dashboard_spec,
  name = "Dashboard",
  filters = c("s_country"),
  overwrite = TRUE
)

runCrunchAutomation(crunch_ds, readr::read_lines("crunch/Dashboard"))