Learn R Programming

RastaRocket (version 1.0.2)

customize_table: Customize a Summary Table with Grouping, Missing Data, and Custom Titles

Description

This function customizes a gtsummary summary table by adding an overall column, handling missing data, applying group-specific statistics, and updating headers and captions. It provides flexible options for grouping, displaying missing data, and customizing table titles.

Usage

customize_table(
  base_table,
  by_group = FALSE,
  var_group,
  add_total,
  show_missing_data,
  show_n_per_group,
  group_title,
  table_title,
  var_title,
  var_tot = NULL,
  var_characteristic = NULL
)

Value

A customized gtsummary table object with added columns, headers, captions, and modifications based on the provided arguments.

Arguments

base_table

A gtsummary table object, typically generated using functions like gtsummary::tbl_summary.

by_group

A boolean (default is FALSE) to analyse by group.

var_group

A string or NULL, specifying the variable used for grouping in the table. If NULL, no group-specific modifications are applied.

add_total

A boolean to add total column or not when var_group is specified.

show_missing_data

A boolean indicating whether to display missing data counts and percentages in the table. If TRUE, columns for missing data will be added.

show_n_per_group

A boolean indicating whether to display group sizes (n) for each level of the grouping variable.

group_title

A string specifying the title for the group column in the table.

table_title

A string specifying the title of the entire table.

var_title

A string specifying the title for the variable column in the table.

var_tot

A string specifying the name of total column. Default is NULL and will guess from theme_gtsummary_language().

var_characteristic

A string specifying the name of characteristic column. Default is NULL and will guess from theme_gtsummary_language().

Details

  • The show_missing_data parameter determines whether missing data counts and percentages are displayed:

    • If TRUE, missing data columns are added.

    • If FALSE, only non-missing data counts are displayed.

  • Headers for columns and spanning headers are customized using the group_title, table_title, and var_title arguments.

Examples

Run this code
# Example usage with a sample gtsummary table
library(gtsummary)
base_table <- trial %>%
  gtsummary::tbl_summary(
    type = list(
      gtsummary::all_continuous() ~ "continuous2"
    ),
    by = "trt",
    missing = "always",
    missing_stat = "{N_nonmiss} ({N_miss})",
    statistic = list(
      gtsummary::all_continuous2() ~ c("{mean} ({sd})",
                                       "{median} ({p25} ; {p75})",
                                       "{min} ; {max}")
    ))
 
customize_table(
  base_table,
  var_group = "trt",
  add_total = TRUE,
  show_missing_data = TRUE,
  show_n_per_group = FALSE,
  group_title = "Treatment Group",
  table_title = "Summary Statistics",
  var_title = "Variables",
  var_tot = "Total"
)

Run the code above in your browser using DataLab