# \donttest{
# Create a dictionary
my_dict <- tibble::tribble(
~variable, ~description,
"age", "Age at Enrollment",
"trt", "Treatment Group",
"grade", "Tumor Grade"
)
# Strip built-in labels so dictionary labels are visible
trial_data <- gtsummary::trial
for (col in names(trial_data)) attr(trial_data[[col]], "label") <- NULL
# Pass dictionary explicitly
trial_data |>
gtsummary::tbl_summary(by = trt, include = c(age, grade)) |>
add_auto_labels(dictionary = my_dict)
# Automatic dictionary search (dictionary in environment)
dictionary <- my_dict
trial_data |>
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
trial_data |>
gtsummary::tbl_summary(
by = trt,
include = c(age, grade),
label = list(age ~ "Custom Age Label") # Manual override
) |>
add_auto_labels(dictionary = my_dict) # grade: dict, age: manual
# }
Run the code above in your browser using DataLab