Skip to contents

Overview

Table shells are tables with generic xx place holders for numeric values. Table shells can be generated by:

  1. Create dummy data or use your own data.

  2. Pass the data to your function of choice.

  3. Overwrite the statistic(s) shown to a fixed character string by implementing the style_xxx() function in the digits argument.

See more details about the digits argument in ?tbl_ae. Here are example specifications.

Integer Specification Function Specification
digits = 1 digits = function(x) style_number(x, digits = 1)
digits = c(0, 1) digits = list(function(x) style_number(x, digits = 0), function(x) style_number(x, digits = 1))
df_patient_characteristics %>%
  select(marker) %>%
  tbl_reg_summary(
    # integer specification
    digits = everything() ~ 1
    # function specification
    # digits = everything() ~ function(x) style_number(x, digits = 1)
  )
#> Setting theme "Compact"
Characteristic N = 100
Biological Marker
    N Non-missing 100.0
    Mean (SD) 5.5 (1.0)
    Median (Q1, Q3) 5.4 (4.8, 6.0)
    Min, Max 3.5, 8.4
    N Missing 0.0

tbl_reg_summary() uniform xx

Here, we specify the digits argument to uniformly apply to all summary statistics in the table. Note that we apply formatting to all columns using the column selector everything().

df_patient_characteristics %>%
  select(marker, trt) %>%
    tbl_reg_summary(
      digits = everything() ~  style_xxx
      ) %>%
    modify_header(stat_0 ~ "**N = xx**")
#> Setting theme "Compact"
Characteristic N = xx1
Biological Marker
    N Non-missing xx
    Mean (SD) xx (xx)
    Median (Q1, Q3) xx (xx, xx)
    Min, Max xx, xx
    N Missing xx
Treatment Group
    Drug A xx (xx%)
    Drug B xx (xx%)
1 n (%)

tbl_reg_summary() custom xx

If you want custom formatting that differs between statistics, implement the digits argument with a list. Categorical variables are selected using all_categorical() and have two statistics to specify (n and %). The multi-line continuous variables are selected with all_continuous() and have nine statistics to specify.

df_patient_characteristics %>%
  select(marker, trt) %>%
    tbl_reg_summary(
      digits = list(
        all_categorical() ~ list(
          style_xxx,                                       # n 
          function(x) style_xxx(x, width = 4, digits = 1)  # %
          ),    
        all_continuous() ~ list(
          style_xxx,                                       # N
          function(x) style_xxx(x, width = 4, digits = 1), # Mean
          function(x) style_xxx(x, width = 4, digits = 1), # SD
          style_xxx,                                       # Median
          style_xxx,                                       # IQR lower
          style_xxx,                                       # IQR upper
          style_xxx,                                       # Range lower
          style_xxx,                                       # Range upper
          style_xxx                                        # N missing
    ))) %>%
    modify_header(stat_0 ~ "**N = xx**")
#> Setting theme "Compact"
Characteristic N = xx1
Biological Marker
    N Non-missing xx
    Mean (SD) xx.x (xx.x)
    Median (Q1, Q3) xx (xx, xx)
    Min, Max xx, xx
    N Missing xx
Treatment Group
    Drug A xx (xx.x%)
    Drug B xx (xx.x%)
1 n (%)

tbl_ae() uniform xx

First, we create some dummy data to fill in our table shell. Note that not all values of grade need to be observed as factor levels populate the tables.

dat_shell <- tribble(
  ~ id,   ~ soc,   ~ ae, ~ grade,
     1, "SOC 1",  "ae1",       1,  
     2, "SOC 1",  "ae2",       1, 
     3, "SOC 2",  "ae3",       1, 
     4, "SOC 2",  "ae4",       1
  ) |> 
  mutate(
    grade = factor(grade, levels = 1:5)
  )

dat_shell
#> # A tibble: 4 × 4
#>      id soc   ae    grade
#>   <dbl> <chr> <chr> <fct>
#> 1     1 SOC 1 ae1   1    
#> 2     2 SOC 1 ae2   1    
#> 3     3 SOC 2 ae3   1    
#> 4     4 SOC 2 ae4   1

Here, we specify the digits argument to uniformly apply to all summary statistics in the table; in this case this is both n and %. As tbl_ae() provides the same summary statistic(s) across all variables, column selectors are not required.

Be sure to use the zero_symbol = NULL option in tbl_ae* functions to print xx’s rather than dashes for zero cells.

dat_shell %>% 
  tbl_ae(
    id = id,
    ae = ae,
    soc = soc,
    by = grade,
    zero_symbol = NULL,
    digits = style_xxx
    ) %>%
  modify_header(all_ae_cols() ~ "**Grade {by}**") %>%
  modify_spanning_header(all_ae_cols() ~ "**N = xx**")
Adverse Event N = xx
Grade 1 Grade 2 Grade 3 Grade 4 Grade 5
SOC 1 xx (xx) xx (xx) xx (xx) xx (xx) xx (xx)
    ae1 xx (xx) xx (xx) xx (xx) xx (xx) xx (xx)
    ae2 xx (xx) xx (xx) xx (xx) xx (xx) xx (xx)
SOC 2 xx (xx) xx (xx) xx (xx) xx (xx) xx (xx)
    ae3 xx (xx) xx (xx) xx (xx) xx (xx) xx (xx)
    ae4 xx (xx) xx (xx) xx (xx) xx (xx) xx (xx)

tbl_ae() custom xx

If you want custom formatting that differs between n and %, specify the digits argument with a list.

dat_shell %>% 
  tbl_ae(
    id = id,
    ae = ae,
    soc = soc,
    by = grade,
    zero_symbol = NULL,
    digits = list(
      style_xxx, #  n
      function(x) style_xxx(x, width = 4, digits = 1) # %
    )) %>%
    modify_header(all_ae_cols() ~ "**Grade {by}**") %>%
    modify_spanning_header(all_ae_cols() ~ "**N = xx**")
Adverse Event N = xx
Grade 1 Grade 2 Grade 3 Grade 4 Grade 5
SOC 1 xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x)
    ae1 xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x)
    ae2 xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x)
SOC 2 xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x)
    ae3 xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x)
    ae4 xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x) xx (xx.x)