code_book() creates an interactive and exportable codebook summarizing all variables of a data frame.
It builds upon varlist() to provide an overview of variable names, labels,
classes, and representative values in a sortable, searchable table.
The output is displayed as an interactive DT::datatable() in the Viewer pane,
allowing filtering, column reordering, and export (copy, print, CSV, Excel, PDF)
directly.
code_book(x, values = FALSE, include_na = FALSE, title = "Codebook", ...)A DT::datatable object.
A data frame or tibble.
Logical. If FALSE (the default), displays a compact summary of the variable's values.
For numeric, character, date/time, labelled, and factor variables, up to four unique non-missing values are shown:
the first three values, followed by an ellipsis (...), and the last value.
Values are sorted when appropriate (e.g., numeric, character, date)
For factors, the levels are used directly and are not sorted.
For labelled variables, prefixed labels are displayed via labelled::to_factor(levels = "prefixed").
If TRUE, all unique non-missing values are displayed.
Logical. If TRUE, unique missing values (NA, NaN) are explicitly appended at the end of the Values summary
when present in the variable. This applies to all variable types.
If FALSE (the default), missing values are omitted from Values but still counted in the NAs column.
Optional character string displayed as the table title in the Viewer.
Defaults to "Codebook". Set to NULL to remove the title completely.
Additional arguments (currently unused).
Requires the following packages:
DT
cli
tools
The interactive datatable supports column sorting, searching, and
client-side export to various formats.
All exports occur client-side through the Viewer or Tab.
varlist() for generating the underlying variable summaries.
if (FALSE) {
# Example with a built-in dataset
df <- head(mtcars)
# Launch the interactive codebook (opens in Viewer)
code_book(df)
}
Run the code above in your browser using DataLab