Chapter 14 Extensions
Here is a non-exhaustive list of packages extending flextable features or allowing flextable output.
14.1 ftExtra
The ftExtra package provides helper functions for the flextable package. For example, colformat_md()
parses markdown texts in columns:
library(ftExtra)
data.frame(
x = c("**bold**", "*italic*"),
y = c("^superscript^", "~subscript~"),
z = c("***^ft^~Extra~** is*", "*Cool*")
) %>%
flextable() %>%
colformat_md()
x |
y |
z |
---|---|---|
**bold** |
^superscript^ |
***^ft^~Extra~** is* |
*italic* |
~subscript~ |
*Cool* |
14.2 crosstable
The crosstable package provides also an elegant and easy way to compute and display descriptive statistics:
library(crosstable)
crosstable(ggplot2::mpg, cols = c("class", "displ"), by = drv) %>%
crosstable::as_flextable()
label |
variable |
drv |
||
---|---|---|---|---|
4 |
f |
r |
||
class |
2seater |
0 (0%) |
0 (0%) |
5 (100.00%) |
compact |
12 (25.53%) |
35 (74.47%) |
0 (0%) |
|
midsize |
3 (7.32%) |
38 (92.68%) |
0 (0%) |
|
minivan |
0 (0%) |
11 (100.00%) |
0 (0%) |
|
pickup |
33 (100.00%) |
0 (0%) |
0 (0%) |
|
subcompact |
4 (11.43%) |
22 (62.86%) |
9 (25.71%) |
|
suv |
51 (82.26%) |
0 (0%) |
11 (17.74%) |
|
displ |
Min / Max |
1.8 / 6.5 |
1.6 / 5.3 |
3.8 / 7.0 |
Med [IQR] |
4.0 [2.9;4.7] |
2.4 [2.0;3.0] |
5.4 [4.6;5.7] |
|
Mean (std) |
4.0 (1.1) |
2.6 (0.7) |
5.2 (0.8) |
|
N (NA) |
103 (0) |
106 (0) |
25 (0) |
As the data is transformed into characters, it is not possible to use conditonnal formatting with these objects.
14.3 gtsummary
The gtsummary package provides an elegant and flexible way to create publication-ready analytical and summary tables using the R programming language. The {gtsummary} package summarizes data sets, regression models, and more, using sensible defaults with customizable capabilities.
library(gtsummary)
trial %>%
select(trt, age, marker) %>%
tbl_summary(
by = trt,
type = all_continuous() ~ "continuous2",
statistic = all_continuous() ~ c("{N_nonmiss}",
"{mean} ({sd})",
"{median} ({p25}, {p75})",
"{min}, {max}"),
missing = "no"
) %>%
italicize_levels() %>%
as_flex_table()
Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
---|---|---|
Age |
<na> |
<na> |
N |
91 |
98 |
Mean (SD) |
47 (15) |
47 (14) |
Median (IQR) |
46 (37, 59) |
48 (39, 56) |
Range |
6, 78 |
9, 83 |
Marker Level (ng/mL) |
<na> |
<na> |
N |
92 |
98 |
Mean (SD) |
1.02 (0.89) |
0.82 (0.83) |
Median (IQR) |
0.84 (0.24, 1.57) |
0.52 (0.19, 1.20) |
Range |
0.00, 3.87 |
0.01, 3.64 |
As the data is transformed into characters, it is not possible to use conditonnal formatting with these objects.