Creates a flextable object from a dataframe using a delimiter to span the header, and allows to easily customise table style.
Usage
formatTable(
x,
type = "gt",
delim = "\n",
style = "default",
na = "-",
title = NULL,
subtitle = NULL,
caption = NULL,
groupColumn = NULL,
groupAsColumn = FALSE,
groupOrder = NULL,
merge = NULL
)
Arguments
- x
A dataframe.
- type
The desired format of the output table. See
tableType()
for allowed options. If "tibble", no formatting will be applied.- delim
Delimiter.
- style
Named list that specifies how to style the different parts of the gt or flextable table generated. Accepted style entries are: title, subtitle, header, header_name, header_level, column_name, group_label, and body. Alternatively, use "default" to get visOmopResults style, or NULL for gt/flextable style. Keep in mind that styling code is different for gt and flextable. To see the "deafult" style code use
tableStyle()
.- na
How to display missing values.
- title
Title of the table, or NULL for no title.
- subtitle
Subtitle of the table, or NULL for no subtitle.
Caption for the table, or NULL for no caption. Text in markdown formatting style (e.g.
*Your caption here*
for caption in italics).- groupColumn
Specifies the columns to use for group labels. By default, the new group name will be a combination of the column names, joined by "_". To assign a custom group name, provide a named list such as: list(
newGroupName
= c("variable_name", "variable_level"))- groupAsColumn
Whether to display the group labels as a column (TRUE) or rows (FALSE).
- groupOrder
Order in which to display group labels.
- merge
Names of the columns to merge vertically when consecutive row cells have identical values. Alternatively, use "all_columns" to apply this merging to all columns, or use NULL to indicate no merging.
Examples
# Example 1
mockSummarisedResult() |>
formatEstimateValue(decimals = c(integer = 0, numeric = 1)) |>
formatHeader(
header = c("Study strata", "strata_name", "strata_level"),
includeHeaderName = FALSE
) |>
formatTable(
type = "flextable",
style = "default",
na = "--",
title = "fxTable example",
subtitle = NULL,
caption = NULL,
groupColumn = "group_level",
groupAsColumn = TRUE,
groupOrder = c("cohort1", "cohort2"),
merge = "all_columns"
)
fxTable example
group_level
result_id
cdm_name
group_name
variable_name
variable_level
estimate_name
estimate_type
additional_name
additional_level
Study strata
overall
age_group &&& sex
sex
age_group
overall
<40 &&& Male
>=40 &&& Male
<40 &&& Female
>=40 &&& Female
Male
Female
<40
>=40
cohort1
1
mock
cohort_name
number subjects
--
count
integer
overall
overall
6,502,987
1,552,945
5,083,099
858,704
5,030,267
385,719
8,240,329
2,859,856
2,467,509
age
--
mean
numeric
overall
overall
24.0
63.9
50.3
25.1
97.9
65.7
9.0
63.1
87.7
sd
numeric
overall
overall
1.7
2.3
8.5
3.5
0.7
0.3
1.2
0.1
7.4
Medications
Amoxiciline
count
integer
overall
overall
25,575
55,232
25,158
62,845
31,612
92,452
90,649
9,197
73,780
percentage
percentage
overall
overall
71.9317963346839
93.6428893357515
42.943161376752
28.618732560426
77.0643183263019
61.6498636547476
15.9274747595191
40.1598005788401
0.164648867212236
Ibuprofen
count
integer
overall
overall
80,930
92,256
67,502
9,397
19,746
47,555
71,437
53,808
87,538
percentage
percentage
overall
overall
54.5915438327938
94.7317183716223
3.8526406744495
64.764387672767
7.96190109103918
43.2930769631639
76.0893133934587
29.3758839834481
10.7331892941147
cohort2
1
mock
cohort_name
number subjects
--
count
integer
overall
overall
3,979,936
6,320,929
9,485,788
3,401,192
6,273,660
8,249,154
1,451,154
9,671,391
860,390
age
--
mean
numeric
overall
overall
93.0
41.9
49.6
35.3
24.9
95.9
36.0
19.9
72.6
sd
numeric
overall
overall
9.2
9.8
4.1
2.7
4.9
0.5
0.0
5.0
2.4
Medications
Amoxiciline
count
integer
overall
overall
56,974
69,649
70,077
3,994
43,152
81,423
56,659
58,825
88,031
percentage
percentage
overall
overall
41.7966035660356
76.3934360351413
79.4477666728199
24.2587020387873
75.9170953882858
2.91041533928365
43.7340782955289
53.0409487430006
42.9831350920722
Ibuprofen
count
integer
overall
overall
29,359
22,477
69,092
31,233
48,330
12,180
69,001
31,573
81,619
percentage
percentage
overall
overall
7.29944417253137
35.6314577395096
57.1560842683539
67.7988059120253
69.0719101810828
37.3410821193829
9.79780545458198
9.90097080357373
77.4801583494991
# Example 2
mockSummarisedResult() |>
formatEstimateValue(decimals = c(integer = 0, numeric = 1)) |>
formatHeader(header = c("Study strata", "strata_name", "strata_level"),
includeHeaderName = FALSE) |>
formatTable(
type = "gt",
style = list("header" = list(
gt::cell_fill(color = "#d9d9d9"),
gt::cell_text(weight = "bold")),
"header_level" = list(gt::cell_fill(color = "#e1e1e1"),
gt::cell_text(weight = "bold")),
"column_name" = list(gt::cell_text(weight = "bold")),
"title" = list(gt::cell_text(weight = "bold"),
gt::cell_fill(color = "#c8c8c8")),
"group_label" = gt::cell_fill(color = "#e1e1e1")),
na = "--",
title = "gtTable example",
subtitle = NULL,
caption = NULL,
groupColumn = "group_level",
groupAsColumn = FALSE,
groupOrder = c("cohort1", "cohort2"),
merge = "all_columns"
)
gtTable example
cohort1
cohort2