Introduction
This vignette focuses on building Crunch dashboards with two helper functions:
-
crunch_generate_tile()builds a single dashboard tile (Crunch DSL). -
crunch_generate_dashboard()assembles tiles into a full dashboard script.
The typical flow is:
- Build a
crunch_dashboard_spectibble withfolderandtile_code. - Generate the automation script with
crunch_generate_dashboard(). - 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:
-
categoricalandmultiple_responsegenerate a bar chart frequency tile. -
categorical_arraygenerates a card with subvariable totals. -
numericgenerates a histogram. -
textgenerates 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_genderUsing 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_weightGrouped-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_byOther 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_byBuild 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 bycrunch_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"))