Generates publication-ready descriptive statistics (Table 1) for selected variables from a REDCap project. Produces n (%), mean (SD), or median (IQR) based on variable type and distribution. Supports stratification by grouping variables and filtering of records.
The function automatically handles character-stored numeric data (common in REDCap) by attempting numeric conversion. Missing values are properly handled and reported.
Usage
generate_table_one(
project,
vars = NULL,
strata = NULL,
filter = NULL,
cat_vars = NULL,
cont_vars = NULL,
non_normal = NULL,
include_missing = TRUE,
test = FALSE,
test_type = "auto",
digits = 1,
output_format = "data.frame",
datatable = FALSE
)Arguments
- project
A redcap_project object
- vars
Character vector of variable names to include in the table. If NULL, all non-ID fields are included (default: NULL)
- strata
Character vector of variable name(s) to stratify by (default: NULL for no stratification)
- filter
Character vector of filter expressions to subset data (e.g., c("age > 18", "consent == 1")). Uses standard R logical expressions. Multiple filters are combined with AND logic. For security, only data frame operations are allowed - system calls and file operations are blocked (default: NULL)
- cat_vars
Character vector of variables to force as categorical (overrides auto-detection)
- cont_vars
Character vector of variables to force as continuous (overrides auto-detection). Character data will be converted to numeric if possible.
- non_normal
Character vector of continuous variables to display as median (IQR) instead of mean (SD) (default: NULL)
- include_missing
Logical. Include missing data counts (default: TRUE)
- test
Logical. Include statistical tests comparing strata (default: FALSE)
- test_type
Character. Type of test: "auto" (default), "parametric", "nonparametric"
- digits
Numeric. Number of decimal places for continuous variables (default: 1)
- output_format
Character. Output format: "data.frame" (default), "kable", "gt", "flextable"
- datatable
Logical. Return output as a DT datatable (default: FALSE)
Examples
if (FALSE) { # \dontrun{
project <- redcap_project()
# Basic table with all variables
table_one <- generate_table_one(project)
# Filtered and stratified
table_one <- generate_table_one(
project,
filter = c("withdrawn != 1", "consent_complete == 1"),
strata = "treatment_group"
)
# Force variable types
table_one <- generate_table_one(
project,
vars = c("age", "gender", "bmi", "study_group"),
cat_vars = c("gender", "study_group"),
cont_vars = c("age", "bmi"),
non_normal = "bmi",
test = TRUE
)
} # }
