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.

title

Title of the table, or NULL for no title.

subtitle

Subtitle of the table, or NULL for no subtitle.

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

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.

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

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
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 1,514,956 3,946,066 9,349,984 7,395,896 6,356,681 3,382,199 9,624,163 1,117,899 6,504,853
age -- mean numeric overall overall 50.1 16.6 36.1 65.2 87.4 72.8 41.2 15.9 84.6
sd numeric overall overall 3.9 9.0 2.8 1.4 4.5 1.1 8.8 5.3 7.4
Medications Amoxiciline count integer overall overall 8,444 3,405 31,096 78,471 28,040 16,528 43,359 81,405 68,651
percentage percentage overall overall 81.5023199422285 30.3833421086892 93.1795864831656 54.043101449497 25.4818350775167 47.7587363682687 70.4463991336524 0.750555354170501 77.2408777615055
Ibuprofen count integer overall overall 32,731 61,475 7,917 51,906 30,628 67,140 39,367 74,785 13,893
percentage percentage overall overall 74.6071676257998 72.3450553836301 61.4524137461558 47.3980569280684 57.8461039112881 24.159372295253 11.4378662081435 29.8344632610679 48.4119494911283
cohort2
1 mock cohort_name number subjects -- count integer overall overall 9,835,116 1,791,275 1,379,704 565,586 5,463,676 4,665,048 8,963,784 185,314 8,509,742
age -- mean numeric overall overall 88.4 39.6 3.8 95.1 78.4 21.9 33.1 17.8 36.3
sd numeric overall overall 7.2 2.6 6.7 2.1 6.9 1.9 7.8 0.2 9.3
Medications Amoxiciline count integer overall overall 77,731 9,120 74,262 39,667 29,603 74,292 46,651 77,303 2,212
percentage percentage overall overall 26.5793564263731 97.104093618691 19.0525120124221 1.96958889719099 61.2376979552209 52.5731408968568 63.4520042454824 39.5447959192097 17.4868220463395
Ibuprofen count integer overall overall 37,734 64,138 99,419 36,856 33,735 91,369 90,942 23,838 41,168
percentage percentage overall overall 56.7406318150461 75.395310902968 54.5425235992298 75.9677961235866 21.2967462604865 83.9265023823828 40.6697055324912 87.3739800183102 88.2156537845731