codebook() creates an interactive HTML data dictionary that opens in the
RStudio Viewer. It shows you everything about your dataset at a glance:
variable names, types, labels, the empirical values found in the data,
value labels, and frequency counts.
Think of it as a professional "cheat sheet" for your dataset -- especially useful when working with labelled survey data imported from SPSS, Stata, or SAS.
codebook(
data,
...,
weights = NULL,
show_id = TRUE,
show_type = TRUE,
show_labels = TRUE,
show_values = TRUE,
show_freq = TRUE,
show_na = TRUE,
show_unused = FALSE,
max_values = 10,
max_len = 50,
sort_by_name = FALSE,
file = NULL,
view = interactive()
)Invisibly returns a list of class "codebook" containing:
Tibble with one row per variable and all metadata
List with dataset-level information (name, nrow, ncol, etc.)
The generated HTML as an htmltools object
Name of the weight variable, or NULL
List of all display options
Named list of frequency tables per variable
Your survey data (a data frame or tibble)
Optional: specific variables to include. If empty, all variables
are shown. Supports tidyselect helpers like starts_with("trust").
Optional survey weights for weighted frequency calculations
Show variable position number? (Default: TRUE)
Show data type? (Default: TRUE)
Show variable labels? (Default: TRUE)
Show empirical values and value labels? (Default: TRUE)
Show frequency counts? (Default: TRUE)
Show tagged missing value types in the codebook? (Default:
TRUE). When data was imported with read_spss(), read_stata(),
read_sas(), or read_xpt() using tagged NAs, they are displayed
with their original missing value codes, labels, and frequencies below
the valid values, separated by a thin gray line.
Show all defined value labels, even those with zero observations? (Default: FALSE). Useful for seeing the full codebook including response options that no respondent selected.
Maximum number of values to display per variable before truncating or showing a range (Default: 10)
Maximum character width for labels before truncation (Default: 50)
Sort variables alphabetically instead of by position? (Default: FALSE)
Path to save the HTML codebook. If NULL (default), no file is
written unless the codebook is opened in the Viewer (then a temporary
file is used). The directory of file must already exist.
Open the HTML codebook in the RStudio Viewer (or browser)?
Defaults to interactive(), so interactive sessions open the Viewer
and scripts/tests do not. Set view = FALSE to suppress the Viewer
side effect entirely; file = writing is unaffected by this argument.
For each variable, the codebook displays (depending on options):
ID: Column position in the dataset
Name: Variable name (in monospace font)
Type: Data type (numeric, factor, ordered factor, haven_labelled, etc.)
Label: Variable label (from SPSS/Stata/SAS imports or manual assignment)
Values: The empirical values found in the data
Value Labels: Labels assigned to those values (if any)
Freq.: Frequency count for each value
Use codebook() when you:
First receive a new dataset and want to understand its structure
Work with labelled data (SPSS, Stata, SAS) and need to see all value labels
Want to document your dataset for colleagues or publications
Need to quickly see value distributions across variables
describe() for detailed numeric summaries, frequency() for
detailed frequency tables, read_spss(), read_por(), read_stata(),
read_sas(), read_xpt() for importing data with tagged NAs
data(survey_data)
# Compact console overview (no Viewer)
cb <- codebook(survey_data)
print(cb)
# Detailed console output
summary(cb)
# \donttest{
# Full codebook (opens in RStudio Viewer when interactive)
codebook(survey_data)
# Only trust-related variables
codebook(survey_data, starts_with("trust"))
# Save to file for sharing
codebook(survey_data, file = tempfile(fileext = ".html"))
# }
Run the code above in your browser using DataLab