| Title: | Produces Publication-Ready Summary Tables |
|---|---|
| Description: | Produces tables with descriptive statistics for continuous, categorical and dichotomous variables. It is largely based on the package 'gtsummary'; Sjoberg DD et al. (2021) <doi:10.32614/RJ-2021-053>. |
| Authors: | Sämi Schär [aut], Charlotte Micheloud [cre, aut] |
| Maintainer: | Charlotte Micheloud <[email protected]> |
| License: | LGPL-3 |
| Version: | 1.0.0 |
| Built: | 2026-10-15 11:57:37 UTC |
| Source: | https://github.com/sakk-statistics/summarysci |
Generates boxplots over time for one or multiple sub-groups.
boxplotSCI( data, var, order = NULL, visit = NULL, group = NULL, group_names = NULL, group_names_short = NULL, main = "", xlab = NULL, ylab = "Variable", ylim = NULL, y_ticks = NULL, col = NULL, cex_lab = 1, cex_axis = 1, cex_n_patients = 0.7, boxwex = NULL, shift = NULL, mar_custom = NULL, xlab_line = NULL, ylab_line = 3, xlab_position = NULL, lines = NULL, lty = NULL, col_lines = NULL, legend_position = "topright", legend_offset = c(0, 0), legend_cex = 1, legend_bty = "n", missing_nr = 0, color_time = FALSE )boxplotSCI( data, var, order = NULL, visit = NULL, group = NULL, group_names = NULL, group_names_short = NULL, main = "", xlab = NULL, ylab = "Variable", ylim = NULL, y_ticks = NULL, col = NULL, cex_lab = 1, cex_axis = 1, cex_n_patients = 0.7, boxwex = NULL, shift = NULL, mar_custom = NULL, xlab_line = NULL, ylab_line = 3, xlab_position = NULL, lines = NULL, lty = NULL, col_lines = NULL, legend_position = "topright", legend_offset = c(0, 0), legend_cex = 1, legend_bty = "n", missing_nr = 0, color_time = FALSE )
data |
A data frame or tibble containing the data to be plotted. |
var |
Name of the column in |
order |
Name of the column in |
visit |
Name of the column in |
group |
Name of the column in |
group_names |
A character vector containing the names of the groups
to be displayed in the legend. The order must match |
group_names_short |
A character vector containing short names or abbreviations
for the groups, to be displayed below the x-axis near the patient counts.
Optional. Defaults to |
main |
Title of the plot. Defaults to |
xlab |
A label for the x-axis. Default is |
ylab |
A label for the y-axis. Default is |
ylim |
The y-axis limits of the plot as a numeric vector of length 2.
Default is |
y_ticks |
A numeric vector specifying the explicit tick positions on the
y-axis. Default is |
col |
A character vector of hex colors or color names to use for the boxes.
Default is |
cex_lab |
A numeric value specifying the font size of the main axis labels. Default is |
cex_axis |
A numeric value specifying the font size of the axis tick marks. Default is |
cex_n_patients |
A numeric value specifying the font size of the sample size
labels and numbers displayed beneath the horizontal axis. Default is |
boxwex |
Scale factor for the width of the boxes. Default is |
shift |
A numeric value indicating the distance between adjacent boxes
within a timepoint cluster. Default is |
mar_custom |
A numeric vector indicating the plot margins to be used in
the form |
xlab_line |
A numeric value indicating the margin line offset for the x-axis label.
Default is |
ylab_line |
A numeric value indicating the margin line offset for the y-axis label.
Default is |
xlab_position |
Horizontal position coordinate adjustment for the sample size
group headers down the left-hand margin. Default is |
lines |
A numeric vector indicating the vertical coordinates where horizontal reference lines will be plotted. Optional. |
lty |
A numeric vector of length 1 or of the same length as |
col_lines |
A character vector of length 1 or of the same length as
|
legend_position |
Position of the legend box. Options include standard base R
keywords (e.g., |
legend_offset |
A numeric vector of length 2 specifying an |
legend_cex |
A numeric value specifying the font and element scaling size
inside the legend box. Default is |
legend_bty |
The type of box border to be drawn around the legend.
Allowed values are |
missing_nr |
A numeric or character value indicating how zero-count patient
sizes on the x-axis should be displayed. Default is |
color_time |
A logical toggle switch enabling legacy timepoint colorization. Supports
legacy use, where, for a single timepoint, multiple groups were used as "timepoint" variable.
If set to |
library(summarySCI) data("hemoglobin_data") boxplotSCI(data = hemoglobin_data, var = hb, order = visit_nr, group = Arm, group_names = c("Treatment (Arm A)", "Control (Arm B)"), group_names_short = c("A", "B"), # Title main = "Hemoglobin over time", # Col of boxes col = c("lightblue", "orange"), # X axis label xlab = "Visits", # X axis position xlab_position = 0, # Y axis label ylab = "Score", lines = c(90, 120, 160), # Col of lines col_lines = c("red", "grey", "red"), # Type of lines lty = c(2, 3, 2) )library(summarySCI) data("hemoglobin_data") boxplotSCI(data = hemoglobin_data, var = hb, order = visit_nr, group = Arm, group_names = c("Treatment (Arm A)", "Control (Arm B)"), group_names_short = c("A", "B"), # Title main = "Hemoglobin over time", # Col of boxes col = c("lightblue", "orange"), # X axis label xlab = "Visits", # X axis position xlab_position = 0, # Y axis label ylab = "Score", lines = c(90, 120, 160), # Col of lines col_lines = c("red", "grey", "red"), # Type of lines lty = c(2, 3, 2) )
boxplot_over_time())A dataset containing the hemoglobin value of 169 patients over 9 visits. Adapted from SAKK 08/14.
data(hemoglobin_data)data(hemoglobin_data)
hemoglobin_dataA data frame with 1307 rows and 5 columns:
Patient ID
Experimental arm
Visit (factor)
Visit (numeric)
Hemoglobin value (g/L)
SAKK
Creates publication-ready summary tables for data grouped, by visit
summaryByVisit( data, vars = NULL, group = NULL, labels = NULL, stat_cont = "median_range", stat_cat = "n_percent", visit = "visit", order = NULL, visitgroup = NULL, digits_cont = 1, digits_cat = 1, missing_percent = TRUE, missing = TRUE, missing_text = "Missing", add_n = FALSE, overall = FALSE, continuous_as_categorical = NULL, as_flex_table = TRUE, border = TRUE, word_output = FALSE, file_path = file_path )summaryByVisit( data, vars = NULL, group = NULL, labels = NULL, stat_cont = "median_range", stat_cat = "n_percent", visit = "visit", order = NULL, visitgroup = NULL, digits_cont = 1, digits_cat = 1, missing_percent = TRUE, missing = TRUE, missing_text = "Missing", add_n = FALSE, overall = FALSE, continuous_as_categorical = NULL, as_flex_table = TRUE, border = TRUE, word_output = FALSE, file_path = file_path )
data |
A data frame or tibble containing the data to be summarized. |
vars |
Variables to include in the summary table.
Need to be specified with quotes, e.g. |
group |
A single column from |
labels |
A list, containing the labels that should be used for the variables in the table. If NULL, labels are automatically taken from the dataset. If no label present, the variable name is taken. |
stat_cont |
Summary statistic to display for continuous variables. Options include "median_IQR", "median_range" (default), "mean_sd", "mean_se" and "geomMean_sd". |
stat_cat |
Summary statistic to display for categorical variables. Options include "n", "n_N" and "n_percent" (default). |
visit |
Name of the stratum for which summary statistics are
displayed by line. Typically, this would be |
order |
A numerical variable defining the visit order. |
visitgroup |
A grouping variable for the stratum for which summary statistics are displayed by line. Must be an ordered factor. Typically, this would be a visit group such as e.g., baseline, follow-up etc. |
digits_cont |
Digits for summary statistics and CI of continuous variables. Default to 1. |
digits_cat |
Digits for summary statistics and CI of categorical variables. Default to 1. |
missing_percent |
Indicates whether percentages for missings are shown (TRUE, default) or not (FALSE) for categorical variables. If "both", then both options are displayed next to each other. |
missing |
Logical. If TRUE (default), the missing values are shown (only for categorical variables). |
missing_text |
String indicating text shown on missing row. Default to "Missing". |
add_n |
Logical. If TRUE, an additional column with the total number of non-missing observations for each variable is added. |
overall |
Logical. If TRUE, an additional column with the total is added to the table. Ignored, if no groups are defined. Default to FALSE. |
continuous_as_categorical |
A subset (vector) of continuous vars to be treated as categrical. Default is NULL. |
as_flex_table |
Logical. If TRUE (default) the gtsummary object is converted to a flextable object. Useful when rendering to Word. |
border |
Logical. If TRUE, a border will be drawn around the table. Only available if flex_table = TRUE. Default is TRUE. |
word_output |
Logical. If TRUE, the table is also saved in a word document. |
file_path |
Character string.
Specify the path of the Word document containing the table.
Only used when |
A table of class "flextable" or c("tbl_strata_nested_stack", "tbl_stack", "gtsummary").
Optionally returns a .docx file in the specified folder.
Creates publication-ready summary tables for categorical data grouped, by visit
summaryByVisitCategorical( data, vars = NULL, group = NULL, labels = NULL, stat_cat = "n_percent", visit = "visit", order = NULL, visitgroup = NULL, digits_cat = 1, missing_percent = TRUE, missing = TRUE, missing_text = "Missing", add_n = FALSE, overall = FALSE, as_flex_table = FALSE, border = TRUE, word_output = FALSE, file_path = paste0("SummaryByVisit_", format(Sys.Date(), "%Y%m%d"), ".docx") )summaryByVisitCategorical( data, vars = NULL, group = NULL, labels = NULL, stat_cat = "n_percent", visit = "visit", order = NULL, visitgroup = NULL, digits_cat = 1, missing_percent = TRUE, missing = TRUE, missing_text = "Missing", add_n = FALSE, overall = FALSE, as_flex_table = FALSE, border = TRUE, word_output = FALSE, file_path = paste0("SummaryByVisit_", format(Sys.Date(), "%Y%m%d"), ".docx") )
data |
A data frame or tibble containing the data to be summarized. |
vars |
Continuous variables to include in the summary table.
Need to be specified with quotes, e.g. |
group |
A single column from |
labels |
A list containing the labels that should be used for the variables in the table. If NULL, labels are automatically taken from the dataset. If no label present, the variable name is taken. |
stat_cat |
Summary statistic to display for categorical variables. Options include "n", "n_N" and "n_percent" (default). |
visit |
Name of the stratum for which summary statistics are
displayed by line. Typically, this would be |
order |
A numerical variable defining the visit order. |
visitgroup |
A grouping variable for the stratum for which summary statistics are displayed by line. Must be an ordered factor. Typically, this would be a visit group such as e.g., baseline, follow-up etc. |
digits_cat |
Digits for summary statistics of categorical variables. Default to 1. |
missing_percent |
Indicates whether percentages for missings are shown (TRUE, default) or not (FALSE) for categorical variables. If "both", then both options are displayed next to each other. |
missing |
Logical. If TRUE (default), the missing values are shown. |
missing_text |
String indicating text shown on missing row. Default to "Missing". |
add_n |
Logical. If TRUE, an additional column with the total number of non-missing observations for each variable is added. |
overall |
Logical. If TRUE, an additional column with the total is added to the table. Ignored, if no groups are defined. Default to FALSE. |
as_flex_table |
Logical. If TRUE (default) the gtsummary object is converted to a flextable object. Useful when rendering to Word. |
border |
Logical. If TRUE, a border will be drawn around the table. Only available if flex_table = TRUE. Default is TRUE. |
word_output |
Logical. If TRUE, the table is also saved in a word document. |
file_path |
Character string.
Specify the path of the Word document containing the table.
Only used when |
A table of class "flextable" or c("tbl_strata_nested_stack", "tbl_stack", "gtsummary").
Optionally returns a .docx file in the specified folder.
Creates publication-ready summary tables for continuous data grouped, by visit
summaryByVisitContinuous( data, vars = NULL, group = NULL, labels = NULL, stat_cont = "median_range", visit = "visit", order = NULL, visitgroup = NULL, digits_cont = 1, add_n = FALSE, overall = FALSE, as_flex_table = FALSE, border = TRUE, word_output = FALSE, file_path = paste0("SummaryByVisit_", format(Sys.Date(), "%Y%m%d"), ".docx") )summaryByVisitContinuous( data, vars = NULL, group = NULL, labels = NULL, stat_cont = "median_range", visit = "visit", order = NULL, visitgroup = NULL, digits_cont = 1, add_n = FALSE, overall = FALSE, as_flex_table = FALSE, border = TRUE, word_output = FALSE, file_path = paste0("SummaryByVisit_", format(Sys.Date(), "%Y%m%d"), ".docx") )
data |
A data frame or tibble containing the data to be summarized. |
vars |
Continuous variables to include in the summary table.
Need to be specified with quotes, e.g. |
group |
A single column from |
labels |
A list containing the labels that should be used for the variables in the table. If NULL, labels are automatically taken from the dataset. If no label present, the variable name is taken. |
stat_cont |
Summary statistic to display for continuous variables. Options include "median_IQR", "median_range" (default), "mean_sd", "mean_se" and "geomMean_sd". |
visit |
Name of the stratum for which summary statistics are
displayed by line. Typically, this would be |
order |
A numerical variable defining the visit order. |
visitgroup |
A grouping variable for the stratum for which summary statistics are displayed by line. Must be an ordered factor. Typically, this would be a visit group such as e.g., baseline, follow-up etc. |
digits_cont |
Digits for summary statistics and CI of continuous variables. Default to 1. |
add_n |
Logical. If TRUE, an additional column with the total number of non-missing observations for each variable is added. |
overall |
Logical. If TRUE, an additional column with the total is added to the table. Ignored, if no groups are defined. Default to FALSE. |
as_flex_table |
Logical. If TRUE (default) the gtsummary object is converted to a flextable object. Useful when rendering to Word. |
border |
Logical. If TRUE, a border will be drawn around the table. Only available if flex_table = TRUE. Default is TRUE. |
word_output |
Logical. If TRUE, the table is also saved in a word document. |
file_path |
Character string.
Specify the path of the Word document containing the table.
Only used when |
A table of class "flextable" or c("tbl_strata_nested_stack", "tbl_stack", "gtsummary").
Optionally returns a .docx file in the specified folder.
Collapses factor levels from multiple columns into one and creates summary table.
summaryLevels( data, vars = NULL, group = NULL, label = NULL, levels = NULL, stat_cat = "n_percent", test = FALSE, test_cat = "fisher.test", ci = FALSE, ci_cat = "wilson", conf_level = 0.95, digits_cat = 0, overall = FALSE, as_flex_table = TRUE, border = TRUE, word_output = FALSE, file_path = paste0("SummaryLevels_", format(Sys.Date(), "%Y%m%d"), ".docx") )summaryLevels( data, vars = NULL, group = NULL, label = NULL, levels = NULL, stat_cat = "n_percent", test = FALSE, test_cat = "fisher.test", ci = FALSE, ci_cat = "wilson", conf_level = 0.95, digits_cat = 0, overall = FALSE, as_flex_table = TRUE, border = TRUE, word_output = FALSE, file_path = paste0("SummaryLevels_", format(Sys.Date(), "%Y%m%d"), ".docx") )
data |
A data frame or tibble containing the data to be summarized. |
vars |
Variables to include in the summary table.
Need to be specified with quotes, e.g. |
group |
A single column from |
label |
A label for the new variable to be created. If no label present, the variable name is taken. |
levels |
= A vector containing the values indicating presence of the factor level. Included by default are "1", "yes", "Yes". |
stat_cat |
Summary statistic to display for categorical variables. Options include "n_percent" (default) and "n", and "n_N". |
test |
Logical. Indicates whether p-values are displayed (TRUE) or not (FALSE). Default to FALSE |
test_cat |
Test type used to calculated the p-value
for categorical variables. Only used if |
ci |
Logical. Indicates whether CI are displayed (TRUE) or not (FALSE). Default to FALSE. |
ci_cat |
Confidence interval method for categorical variables. Options include "wilson" (default), "wilson.no.correct", "clopper.pearson", "wald", "wald.no.correct", "agresti.coull" and "jeffreys". If NULL, no CI will be displayed. |
conf_level |
Numeric. Confidence level. Default to 0.95. |
digits_cat |
Numeric. Digits for summary statistics and CI of categorical variables. Default to 0. |
overall |
Logical. If TRUE, an additional column with the total is added to the table. Default to FALSE. |
as_flex_table |
Logical. If TRUE (default) the gtsummary object is converted to a flextable object. Useful when rendering to Word. |
border |
Logical. If TRUE, a border will be drawn around the table. Only available if flex_table = TRUE. Default is TRUE. |
word_output |
Logical. If TRUE, the table is also saved in a word document. |
file_path |
Character string.
Specify the path of the Word document containing the table.
Only used when |
A table of class "flextable" or c("tbl_stack", "gtsummary").
Optionally returns a .docx file in the specified folder.
Creates publication-ready summary tables based on the gtsummary package.
summaryTable( data, vars = NULL, group = NULL, labels = NULL, stat_cont = "median_range", stat_cat = "n_percent", continuous_as = "continuous", dichotomous_as = "dichotomous", continuous_as_categorical = NULL, dichotomous_as_categorical = NULL, ref_level = NULL, test = FALSE, test_cont = NULL, test_cat = "fisher.test", ci = FALSE, ci_cont = "wilcox.test", ci_cat = "clopper.pearson", conf_level = 0.95, digits_cont = 1, digits_cat = 0, missing = TRUE, missing_percent = TRUE, missing_text = "Missing", overall = FALSE, add_n = TRUE, as_flex_table = TRUE, border = TRUE, word_output = FALSE, file_path = paste0("SummaryTable_", format(Sys.Date(), "%Y%m%d"), ".docx") )summaryTable( data, vars = NULL, group = NULL, labels = NULL, stat_cont = "median_range", stat_cat = "n_percent", continuous_as = "continuous", dichotomous_as = "dichotomous", continuous_as_categorical = NULL, dichotomous_as_categorical = NULL, ref_level = NULL, test = FALSE, test_cont = NULL, test_cat = "fisher.test", ci = FALSE, ci_cont = "wilcox.test", ci_cat = "clopper.pearson", conf_level = 0.95, digits_cont = 1, digits_cat = 0, missing = TRUE, missing_percent = TRUE, missing_text = "Missing", overall = FALSE, add_n = TRUE, as_flex_table = TRUE, border = TRUE, word_output = FALSE, file_path = paste0("SummaryTable_", format(Sys.Date(), "%Y%m%d"), ".docx") )
data |
A data frame or tibble containing the data to be summarized. |
vars |
Variables to include in the summary table.
Need to be specified with quotes, e.g. |
group |
A single column from |
labels |
A list containing the labels that should be used for the variables in the table. If NULL, labels are automatically taken from the dataset. If no label present, the variable name is taken. |
stat_cont |
Summary statistic to display for continuous variables. Options include "median_IQR", "median_range" (default), "mean_sd", "mean_se" and "geomMean_sd". |
stat_cat |
Summary statistic to display for categorical variables. Options include "n_percent" (default) and "n", and "n_N". |
continuous_as |
Type for all the continuous variables. Can either be "continuous" (default) or "categorical". |
dichotomous_as |
Type for the dichotomous variables. Can either be
"categorical" (one row per level) or "dichotomous" (only
one row with reference level (see argument |
continuous_as_categorical |
A subset (vector) of continuous vars to be treated as categorical. Default is NULL. |
dichotomous_as_categorical |
A subset (vector) of dichotomous vars to be treated as categorical. Default is NULL. |
ref_level |
Specifies the reference level of a variable to display on a single row.
Default is the first appearing level. The syntax is as follows: |
test |
Logical. Indicates whether p-values are displayed (TRUE) or not (FALSE). Default to FALSE |
test_cont |
Test type used to calculate the p-value
for continuous variables. Only used if |
test_cat |
Test type used to calculated the p-value
for categorical variables. Only used if |
ci |
Logical. Indicates whether CI are displayed (TRUE) or not (FALSE). Default to FALSE. |
ci_cont |
Confidence interval method for continuous variables.
Only used if |
ci_cat |
Confidence interval method for categorical variables. Options include "clopper.pearson" (default) "wilson", "wilson.no.correct", "wald", "wald.no.correct", "agresti.coull" and "jeffreys". If NULL, no CI will be displayed. |
conf_level |
Numeric. Confidence level. Default to 0.95. |
digits_cont |
Numeric. Digits for summary statistics and CI of continuous variables. Default to 1. |
digits_cat |
Numeric. Digits for summary statistics and CI of categorical variables. Default to 0. |
missing |
Logical. If TRUE (default), the missing values are shown. |
missing_percent |
Indicates whether percentages for missings are shown (TRUE, default) or not (FALSE) for categorical variables. If "both", then both options are displayed next to each other. |
missing_text |
String indicating text shown on missing row. Default to "Missing". |
overall |
Logical. If TRUE, an additional column with the total is added to the table. Default to FALSE. |
add_n |
Logical. If TRUE (default), an additional column with the total number of non-missing observations for each variable is added. |
as_flex_table |
Logical. If TRUE (default) the gtsummary object is converted to a flextable object. Useful when rendering to Word. |
border |
Logical. If TRUE, a border will be drawn around the table. Only available if flex_table = TRUE. Default is TRUE. |
word_output |
Logical. If TRUE, the table is also saved in a word document. |
file_path |
Character string.
Specify the path of the Word document containing the table.
Only used when |
A table of class "flextable" or c("tbl_summary", "gtsummary").
Optionally returns a .docx file in the specified folder.
library(survival) data("cancer") summaryTable(data = cancer,vars = c("inst", "time","age", "ph.ecog"), labels = list(inst = "Institution code", time = "Time", age = "Age", ph.ecog = "ECOG score"))library(survival) data("cancer") summaryTable(data = cancer,vars = c("inst", "time","age", "ph.ecog"), labels = list(inst = "Institution code", time = "Time", age = "Age", ph.ecog = "ECOG score"))