This function leverages gtsummary and gt to generate a publication-quality summary table. It is designed to be robust, highly customizable, and easy to integrate into R Markdown workflows for both HTML and PDF output.

create_table1(
  data,
  by_var,
  vars,
  font_size = NULL,
  title = NULL,
  subtitle = NULL,
  footnote = NULL,
  p_value = FALSE,
  missing_text = "Unknown",
  statistic = list(gtsummary::all_continuous() ~ "{median} ({p25}, {p75})"),
  digits = NULL,
  type = list(dplyr::where(is.numeric) ~ "continuous")
)

Arguments

data

A data frame or tibble containing the data to summarize.

by_var

A character string specifying the stratifying column name. Required.

vars

A character vector of column names to be summarized. Required.

font_size

A numeric value for the table's font size. gt will handle translation to appropriate units (e.g., px for HTML, pt for LaTeX). Example: 10. If NULL (the default), gt's standard size is used.

title

A character string for the table's main title.

subtitle

A character string for the table's subtitle.

footnote

A character string for a source note or footnote.

p_value

Logical. If TRUE, an overall p-value is added.

missing_text

A character string to display for missing values.

statistic

A named list specifying the statistics to report.

digits

A named list specifying the number of digits to display.

type

A named list of formulas specifying variable types. Default list(where(is.numeric) ~ "continuous") correctly handles all numeric variables.

Value

A gt_tbl object.

Examples

if (require(gtsummary) && require(gt) && require(dplyr)) {
  # Example 1: Standard usage
  create_table1(
    data = gtsummary::trial,
    by_var = "trt",
    vars = c("age", "grade")
  )

  # Example 2: Using the font_size argument for a more compact table
  create_table1(
    data = gtsummary::trial,
    by_var = "trt",
    vars = c("age", "grade", "response", "marker"),
    title = "Patient Demographics",
    font_size = 9 # Set font size to 9pt
  )
}
#> Loading required package: gtsummary
#> Loading required package: gt
#> Loading required package: dplyr
#> 
#> Attaching package: ‘dplyr’
#> The following object is masked from ‘package:Biobase’:
#> 
#>     combine
#> The following objects are masked from ‘package:BiocGenerics’:
#> 
#>     combine, intersect, setdiff, union
#> The following objects are masked from ‘package:stats’:
#> 
#>     filter, lag
#> The following objects are masked from ‘package:base’:
#> 
#>     intersect, setdiff, setequal, union
Patient Demographics
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
Age 46 (37, 60) 48 (39, 56)
    Unknown 7 4
Grade

    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response 0 (0, 1) 0 (0, 1)
    Unknown 3 4
Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21)
    Unknown 6 4
1 Median (Q1, Q3); n (%)