# \donttest{
# Create a dictionary
my_dict <- tibble::tribble(
~Variable, ~Description,
"age", "Age at Enrollment",
"trt", "Treatment Group",
"grade", "Tumor Grade"
)
# Basic usage: pass dictionary explicitly
gtsummary::trial |>
gtsummary::tbl_summary(by = trt, include = c(age, grade)) |>
add_auto_labels(dictionary = my_dict)
# Automatic dictionary search (dictionary in environment)
dictionary <- my_dict
gtsummary::trial |>
gtsummary::tbl_summary(by = trt, include = c(age, grade)) |>
add_auto_labels() # Finds dictionary automatically
# Working with pre-labeled data (no dictionary needed)
labeled_data <- gtsummary::trial
attr(labeled_data$age, "label") <- "Patient Age (years)"
attr(labeled_data$marker, "label") <- "Marker Level (ng/mL)"
labeled_data |>
gtsummary::tbl_summary(include = c(age, marker)) |>
add_auto_labels() # Reads from label attributes
# Manual overrides always win
gtsummary::trial |>
gtsummary::tbl_summary(
by = trt,
include = c(age, grade),
label = list(age ~ "Custom Age Label") # Manual override
) |>
add_auto_labels(dictionary = my_dict) # grade gets dict label, age keeps manual
# Control priority with options
options(sumExtras.preferDictionary = TRUE) # Dictionary over attributes
# Data has both dictionary and attributes
labeled_trial <- gtsummary::trial
attr(labeled_trial$age, "label") <- "Age from Attribute"
dictionary <- tibble::tribble(
~Variable, ~Description,
"age", "Age from Dictionary"
)
labeled_trial |>
gtsummary::tbl_summary(include = age) |>
add_auto_labels() # Uses "Age from Dictionary" (option = TRUE)
# }
Run the code above in your browser using DataLab