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. Not used for "datatable".
- title
Title of the table, or NULL for no title. Not used for "datatable".
- subtitle
Subtitle of the table, or NULL for no subtitle. Not used for "datatable".
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). Not used for "datatable".
- groupOrder
Order in which to display group labels. Not used for "datatable".
- 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. Not used for "datatable".
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
1,333,485
9,272,818
8,941,227
2,036,644
2,569,829
6,140,056
4,414,992
3,155,947
1,013,138
age
--
mean
numeric
overall
overall
26.2
41.6
43.9
18.2
7.8
1.8
72.8
97.1
53.5
sd
numeric
overall
overall
9.6
4.5
9.8
5.4
3.6
5.0
0.7
9.7
6.5
Medications
Amoxiciline
count
integer
overall
overall
45,675
17,903
96,621
71,341
2,489
62,646
82,792
64,307
70,848
percentage
percentage
overall
overall
0.865807477384806
43.0898792576045
72.493509715423
41.2256387062371
55.0862395204604
15.6360570108518
4.30012932047248
68.464411306195
51.4402497094125
Ibuprofen
count
integer
overall
overall
34,970
98,267
40,885
36,087
65,251
14,356
68,080
85,929
83,863
percentage
percentage
overall
overall
11.3047500839457
97.0262279734015
64.8082781815901
5.6073043262586
47.3137095803395
29.4625013135374
61.0299621941522
12.1108880266547
62.9416507901624
cohort2
1
mock
cohort_name
number subjects
--
count
integer
overall
overall
2,726,462
6,536,994
9,278,701
266,247
5,594,912
8,541,737
758,830
2,058,289
4,958,896
age
--
mean
numeric
overall
overall
16.6
5.8
45.9
62.3
59.5
74.3
52.4
49.7
1.5
sd
numeric
overall
overall
9.8
5.4
7.6
5.6
7.8
6.4
3.8
3.6
8.4
Medications
Amoxiciline
count
integer
overall
overall
87,073
28,181
17,322
10,967
58,622
85,072
92,153
67,566
35,059
percentage
percentage
overall
overall
94.568784837611
84.309694217518
6.48830933496356
76.2110914569348
7.83842522650957
13.3507662685588
80.9696201002225
55.9383239364251
73.3729686122388
Ibuprofen
count
integer
overall
overall
88,131
23,770
57,863
89,790
34,443
76,086
73,110
85,243
97,582
percentage
percentage
overall
overall
71.1955302860588
61.2082786625251
3.44292330555618
66.5890787029639
65.2967276517302
45.1858439715579
51.6804101876915
67.6610797178
88.0933094071224
# 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