knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(gtreg) library(dplyr) gtsummary::theme_gtsummary_compact()
Table shells are tables with generic xx
place holders for numeric
values. Table shells can be generated by:
Create dummy data or use your own data.
Pass the data to your function of choice.
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.
tibble::tribble( ~`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))`" ) |> knitr::kable()
df_patient_characteristics %>% select(marker) %>% tbl_reg_summary( # integer specification digits = everything() ~ 1 # function specification # digits = everything() ~ function(x) style_number(x, digits = 1) )
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**")
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**")
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
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**")
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**")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.