library(DemographicTable)
library(flextable)
set_flextable_defaults(font.size = 9)
This vignette of package DemographicTable
(CRAN, Github) presents an idiot-proof interface to create a summary table of simple statistics, often known as a demographic table.
Examples in this vignette require that the search
path has
library(DemographicTable)
library(flextable)
set_flextable_defaults(font.size = 9)
Users may remove the last pipe |> as_flextable()
from all examples. This is required in the vignette to make quarto
rendering work.
Data preparation
= ToothGrowth |>
tgr within.data.frame(expr = {
= factor(dose)
dose })
|>
tgr DemographicTable(include = c('supp', 'len', 'dose')) |>
as_flextable()
group
Color of each individual group
is determined by scales::pal_hue()
, which is the default color pallete used in package ggplot2
.
|>
tgr DemographicTable(groups = 'supp', include = c('len', 'dose')) |>
as_flextable()
User may choose to hide the -values with option compare = FALSE
.
|>
tgr DemographicTable(groups = 'supp', include = c('len', 'dose'), compare = FALSE) |>
as_flextable()
groups
|>
tgr DemographicTable(groups = c('supp', 'dose'), include = c('len', 'supp')) |>
as_flextable()
DemographicTable
s= CO2 |>
tb1 DemographicTable(groups = 'Type', include = c('conc', 'uptake'))
= CO2 |>
tb2 subset(subset = (Treatment == 'nonchilled')) |>
DemographicTable(groups = 'Type', include = c('conc', 'uptake'), data.name = 'CO2_nonchilled')
c(tb1, tb2) |> as_flextable()
groups
::survey |>
MASSDemographicTable(groups = c('M.I'), include = c('Pulse', 'Fold')) |>
as_flextable()
logical
valuesUsing logical
values is discouraged, as this practice is proved confusing to scientists without a strong data background.
= mtcars |>
mtc within.data.frame(expr = {
= as.logical(vs)
vs_straight = as.logical(am)
am_manual })
A warning message will be printed if logical
variables are used in groups
and/or include
.
tryCatch(DemographicTable(mtc, groups = 'am_manual', include = c('drat', 'vs_straight')), warning = identity)
<simpleWarning in DemographicTable(mtc, groups = "am_manual", include = c("drat", "vs_straight")):
Some scientists do not understand logical value (e.g., arm_intervention being TRUE/FALSE)
Consider using 2-level factor (e.g., arm being intervention/control)>
Instead of using logical
variables
|>
mtc DemographicTable(groups = 'am_manual', include = c('drat', 'vs_straight')) |>
as_flextable() |>
suppressWarnings()
We recommend using 2-level
factor
s.
|>
mtcars within.data.frame(expr = {
= ifelse(vs, yes = 'Straight', no = 'V-shaped')
vs = ifelse(am, yes = 'manual', no = 'automatic')
am |>
}) DemographicTable(groups = 'am', include = c('drat', 'vs'), data.name = 'mtcars') |>
as_flextable()