Skip to contents

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

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".

Value

A flextable object.

A flextable or gt object.

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
Study strata
result_id cdm_name group_name variable_name variable_level estimate_name estimate_type additional_name additional_level
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 7,312,292 3,986,198 4,305,207 1,454,073 4,511,203 5,787,815 704,171 7,423,262 5,492,207
age -- mean numeric overall overall 77.8 65.8 25.9 91.8 14.9 64.1 20.1 74.4 84.8
sd numeric overall overall 4.4 9.0 7.7 1.3 5.4 1.3 5.1 5.4 2.4
Medications Amoxiciline count integer overall overall 36,924 12,667 73,987 95,587 66,867 46,809 53,819 33,095 62,868
percentage percentage overall overall 6.84064193628728 95.8503520581871 24.4483520975336 92.6283334614709 50.2747076796368 93.5517142061144 43.8799632480368 35.0486536743119 47.6504088379443
Ibuprofen count integer overall overall 13,002 79,191 35,590 96,434 11,868 45,816 52,365 83,706 3,350
percentage percentage overall overall 75.4406645428389 24.2454329272732 62.715786579065 16.8414602987468 44.4789632922038 16.4675234118477 11.8276783032343 36.7464390583336 94.6558905066922
cohort2
1 mock cohort_name number subjects -- count integer overall overall 6,345,443 1,366,286 9,302,335 6,511,186 5,287,093 2,251,154 9,832,669 1,090,488 699,455
age -- mean numeric overall overall 85.4 80.3 2.4 79.5 88.7 60.0 43.3 92.0 61.6
sd numeric overall overall 8.0 9.5 4.1 5.1 9.8 3.5 4.6 2.2 0.8
Medications Amoxiciline count integer overall overall 45,255 88,802 78,420 13,806 28,131 70,695 44,219 67,770 70,071
percentage percentage overall overall 96.6159525094554 29.7161926981062 29.0648308349773 85.9722247580066 19.3312666844577 80.9108371380717 36.5486512426287 25.2699653152376 85.6081013800576
Ibuprofen count integer overall overall 4,333 79,210 20,270 6,880 86,444 7,709 43,100 97,362 57,096
percentage percentage overall overall 87.5410763313994 26.0058912448585 65.7298437319696 12.1864625485614 31.8848039023578 62.3061030171812 44.6997430175543 70.8293445874006 89.0028999885544