5  ‘flextable’ et le package ‘tables’

5.1 tables

Le package ‘tables’ https://dmurdoch.github.io/tables/ fournit une interface basée sur un système de formules pour calculer le contenu des tableaux et pour les formater.

Il a été très inspiré par SAS PROC TABULATE.

L’utilisateur peut créer un objet tableau en spécifiant une formule, avec le côté gauche indiquant les lignes et le côté droit indiquant les colonnes. La formule décrit les fonctions de résumé à appliquer et la manière de les organiser. Les objets peuvent être filtrés ou combinés en utilisant des opérations similaires à celles des matrices. Les tableaux peuvent être rendus sous forme de texte brut, de code LaTeX pour apparaître dans un document PDF, ou de code HTML pour un document web.

5.2 Un exemple de tableau d’évènements indésirables

On va avoir besoin de deux tables “cliniques”.

Code
ex_adae <- formatters::ex_adae
ex_adae

STUDYID

USUBJID

SUBJID

SITEID

AGE

SEX

RACE

COUNTRY

INVID

ARM

ARMCD

ACTARM

ACTARMCD

STRATA1

STRATA2

BMRKR1

BMRKR2

ITTFL

SAFFL

BMEASIFL

BEP01FL

RANDDT

TRTSDTM

TRTEDTM

EOSSTT

EOSDT

EOSDY

DCSREAS

DTHDT

LSTALVDT

study_duration_secs

ASEQ

AESEQ

AETERM

AELLT

AEDECOD

AEHLT

AEHLGT

AEBODSYS

AESOC

AESEV

AESER

AREL

ASTDTM

AENDTM

ASTDY

AENDY

AETOXGR

character

character

character

character

integer

factor

factor

factor

character

factor

factor

factor

factor

factor

factor

numeric

factor

factor

factor

factor

factor

Date

POSIXct

POSIXct

factor

Date

integer

factor

Date

Date

numeric

integer

integer

factor

factor

factor

factor

factor

factor

factor

factor

factor

factor

POSIXct

POSIXct

integer

integer

factor

AB12345

AB12345-BRA-1-id-134

id-134

BRA-1

47

M

WHITE

BRA

BRA-1

A: Drug X

ARM A

A: Drug X

ARM A

B

S2

6.5

LOW

Y

Y

Y

N

2021-06-09

2021-06-10 13:26:53

2023-06-11 01:05:17

COMPLETED

2023-06-11

731

2023-06-11

2023-06-29

63,113,904

1

1

trm B.2.1.2.1

llt B.2.1.2.1

dcd B.2.1.2.1

hlt B.2.1.2

hlgt B.2.1

cl B.2

cl B

MODERATE

N

N

2022-02-16 00:00:00

2022-11-10 00:00:00

251

518

3

AB12345

AB12345-BRA-1-id-134

id-134

BRA-1

47

M

WHITE

BRA

BRA-1

A: Drug X

ARM A

A: Drug X

ARM A

B

S2

6.5

LOW

Y

Y

Y

N

2021-06-09

2021-06-10 13:26:53

2023-06-11 01:05:17

COMPLETED

2023-06-11

731

2023-06-11

2023-06-29

63,113,904

2

2

trm D.1.1.4.2

llt D.1.1.4.2

dcd D.1.1.4.2

hlt D.1.1.4

hlgt D.1.1

cl D.1

cl D

MODERATE

N

N

2022-04-10 00:00:00

2022-12-21 00:00:00

304

559

3

AB12345

AB12345-BRA-1-id-134

id-134

BRA-1

47

M

WHITE

BRA

BRA-1

A: Drug X

ARM A

A: Drug X

ARM A

B

S2

6.5

LOW

Y

Y

Y

N

2021-06-09

2021-06-10 13:26:53

2023-06-11 01:05:17

COMPLETED

2023-06-11

731

2023-06-11

2023-06-29

63,113,904

3

3

trm A.1.1.1.2

llt A.1.1.1.2

dcd A.1.1.1.2

hlt A.1.1.1

hlgt A.1.1

cl A.1

cl A

MODERATE

Y

N

2022-10-20 00:00:00

2023-06-05 00:00:00

497

725

2

AB12345

AB12345-BRA-1-id-134

id-134

BRA-1

47

M

WHITE

BRA

BRA-1

A: Drug X

ARM A

A: Drug X

ARM A

B

S2

6.5

LOW

Y

Y

Y

N

2021-06-09

2021-06-10 13:26:53

2023-06-11 01:05:17

COMPLETED

2023-06-11

731

2023-06-11

2023-06-29

63,113,904

4

4

trm A.1.1.1.2

llt A.1.1.1.2

dcd A.1.1.1.2

hlt A.1.1.1

hlgt A.1.1

cl A.1

cl A

MODERATE

Y

N

2023-02-08 00:00:00

2023-04-15 00:00:00

608

674

2

AB12345

AB12345-BRA-1-id-141

id-141

BRA-1

35

F

WHITE

BRA

BRA-1

C: Combination

ARM C

C: Combination

ARM C

B

S1

7.5

HIGH

Y

Y

Y

Y

2021-02-25

2021-02-28 23:47:16

2023-03-01 11:25:40

COMPLETED

2023-03-01

731

2023-03-01

2023-03-30

63,113,904

1

1

trm B.2.1.2.1

llt B.2.1.2.1

dcd B.2.1.2.1

hlt B.2.1.2

hlgt B.2.1

cl B.2

cl B

MODERATE

N

N

2021-11-14 00:00:00

2021-11-21 00:00:00

259

266

3

AB12345

AB12345-BRA-1-id-141

id-141

BRA-1

35

F

WHITE

BRA

BRA-1

C: Combination

ARM C

C: Combination

ARM C

B

S1

7.5

HIGH

Y

Y

Y

Y

2021-02-25

2021-02-28 23:47:16

2023-03-01 11:25:40

COMPLETED

2023-03-01

731

2023-03-01

2023-03-30

63,113,904

2

2

trm D.2.1.5.3

llt D.2.1.5.3

dcd D.2.1.5.3

hlt D.2.1.5

hlgt D.2.1

cl D.2

cl D

MILD

N

Y

2021-12-28 00:00:00

2023-02-21 00:00:00

303

723

1

AB12345

AB12345-BRA-1-id-141

id-141

BRA-1

35

F

WHITE

BRA

BRA-1

C: Combination

ARM C

C: Combination

ARM C

B

S1

7.5

HIGH

Y

Y

Y

Y

2021-02-25

2021-02-28 23:47:16

2023-03-01 11:25:40

COMPLETED

2023-03-01

731

2023-03-01

2023-03-30

63,113,904

3

3

trm A.1.1.1.1

llt A.1.1.1.1

dcd A.1.1.1.1

hlt A.1.1.1

hlgt A.1.1

cl A.1

cl A

MILD

N

N

2022-07-06 00:00:00

2022-07-29 00:00:00

493

516

1

AB12345

AB12345-BRA-1-id-141

id-141

BRA-1

35

F

WHITE

BRA

BRA-1

C: Combination

ARM C

C: Combination

ARM C

B

S1

7.5

HIGH

Y

Y

Y

Y

2021-02-25

2021-02-28 23:47:16

2023-03-01 11:25:40

COMPLETED

2023-03-01

731

2023-03-01

2023-03-30

63,113,904

4

4

trm A.1.1.1.2

llt A.1.1.1.2

dcd A.1.1.1.2

hlt A.1.1.1

hlgt A.1.1

cl A.1

cl A

MODERATE

Y

N

2022-10-21 00:00:00

2023-01-22 00:00:00

600

693

2

AB12345

AB12345-BRA-1-id-141

id-141

BRA-1

35

F

WHITE

BRA

BRA-1

C: Combination

ARM C

C: Combination

ARM C

B

S1

7.5

HIGH

Y

Y

Y

Y

2021-02-25

2021-02-28 23:47:16

2023-03-01 11:25:40

COMPLETED

2023-03-01

731

2023-03-01

2023-03-30

63,113,904

5

5

trm A.1.1.1.1

llt A.1.1.1.1

dcd A.1.1.1.1

hlt A.1.1.1

hlgt A.1.1

cl A.1

cl A

MILD

N

N

2022-11-25 00:00:00

2023-01-07 00:00:00

635

678

1

AB12345

AB12345-BRA-1-id-141

id-141

BRA-1

35

F

WHITE

BRA

BRA-1

C: Combination

ARM C

C: Combination

ARM C

B

S1

7.5

HIGH

Y

Y

Y

Y

2021-02-25

2021-02-28 23:47:16

2023-03-01 11:25:40

COMPLETED

2023-03-01

731

2023-03-01

2023-03-30

63,113,904

6

6

trm D.1.1.1.1

llt D.1.1.1.1

dcd D.1.1.1.1

hlt D.1.1.1

hlgt D.1.1

cl D.1

cl D

SEVERE

Y

N

2023-02-28 00:00:00

2023-03-01 00:00:00

730

731

5

n: 1934

Code
ex_adsl <- formatters::ex_adsl
ex_adsl

STUDYID

USUBJID

SUBJID

SITEID

AGE

SEX

RACE

COUNTRY

INVID

ARM

ARMCD

ACTARM

ACTARMCD

STRATA1

STRATA2

BMRKR1

BMRKR2

ITTFL

SAFFL

BMEASIFL

BEP01FL

RANDDT

TRTSDTM

TRTEDTM

EOSSTT

EOSDT

EOSDY

DCSREAS

DTHDT

LSTALVDT

study_duration_secs

character

character

character

character

integer

factor

factor

factor

character

factor

factor

factor

factor

factor

factor

numeric

factor

factor

factor

factor

factor

Date

POSIXct

POSIXct

factor

Date

integer

factor

Date

Date

numeric

AB12345

AB12345-CHN-3-id-128

id-128

CHN-3

32

M

ASIAN

CHN

CHN-3

A: Drug X

ARM A

A: Drug X

ARM A

C

S2

14.4

MEDIUM

Y

Y

Y

Y

2019-09-28

2019-09-30 20:45:38

2021-09-30 08:24:02

COMPLETED

2021-09-30

731

2021-09-30

2021-10-22

63,113,904

AB12345

AB12345-CHN-15-id-262

id-262

CHN-15

35

M

BLACK OR AFRICAN AMERICAN

CHN

CHN-15

C: Combination

ARM C

C: Combination

ARM C

C

S1

4.1

LOW

Y

Y

N

N

2019-10-02

2019-10-02 18:41:20

2021-10-02 06:19:44

COMPLETED

2021-10-02

731

2021-10-02

2021-10-24

63,113,904

AB12345

AB12345-RUS-3-id-378

id-378

RUS-3

30

F

ASIAN

RUS

RUS-3

C: Combination

ARM C

C: Combination

ARM C

A

S1

2.8

HIGH

Y

Y

Y

N

2019-09-30

2019-10-04 12:55:28

2021-10-04 00:33:52

COMPLETED

2021-10-04

731

2021-10-04

2021-10-15

63,113,904

AB12345

AB12345-CHN-11-id-220

id-220

CHN-11

26

F

ASIAN

CHN

CHN-11

B: Placebo

ARM B

B: Placebo

ARM B

B

S2

10.3

MEDIUM

Y

Y

Y

Y

2019-10-03

2019-10-05 23:09:23

2021-10-05 10:47:47

COMPLETED

2021-10-05

731

2021-10-05

2021-10-24

63,113,904

AB12345

AB12345-CHN-7-id-267

id-267

CHN-7

40

M

ASIAN

CHN

CHN-7

B: Placebo

ARM B

B: Placebo

ARM B

C

S1

6.2

LOW

Y

Y

N

N

2019-10-05

2019-10-06 09:45:36

2021-10-05 21:24:00

COMPLETED

2021-10-05

731

2021-10-05

2021-11-03

63,113,904

AB12345

AB12345-CHN-15-id-201

id-201

CHN-15

49

M

ASIAN

CHN

CHN-15

C: Combination

ARM C

C: Combination

ARM C

C

S2

6.9

MEDIUM

Y

Y

Y

N

2019-10-10

2019-10-10 01:00:04

2021-09-24 06:00:13

DISCONTINUED

2021-09-24

716

WITHDRAWAL BY PARENT/GUARDIAN

2021-09-24

2021-10-22

63,113,904

AB12345

AB12345-USA-1-id-45

id-45

USA-1

34

F

ASIAN

USA

USA-1

A: Drug X

ARM A

A: Drug X

ARM A

C

S1

0.5

LOW

Y

Y

N

N

2019-10-08

2019-10-10 16:08:24

2021-10-10 03:46:48

COMPLETED

2021-10-10

731

2021-10-10

2021-10-24

63,113,904

AB12345

AB12345-USA-1-id-261

id-261

USA-1

32

F

ASIAN

USA

USA-1

B: Placebo

ARM B

B: Placebo

ARM B

C

S1

2.9

HIGH

Y

Y

Y

N

2019-10-07

2019-10-11 02:57:15

2021-10-10 14:35:39

COMPLETED

2021-10-10

731

2021-10-10

2021-10-26

63,113,904

AB12345

AB12345-NGA-11-id-173

id-173

NGA-11

24

F

BLACK OR AFRICAN AMERICAN

NGA

NGA-11

C: Combination

ARM C

C: Combination

ARM C

C

S2

5.0

LOW

Y

Y

Y

Y

2019-10-12

2019-10-14 19:11:18

2021-10-14 06:49:42

COMPLETED

2021-10-14

731

2021-10-14

2021-10-25

63,113,904

AB12345

AB12345-CHN-1-id-307

id-307

CHN-1

24

M

ASIAN

CHN

CHN-1

B: Placebo

ARM B

B: Placebo

ARM B

C

S1

4.6

LOW

Y

Y

Y

N

2019-10-12

2019-10-15 17:37:13

2021-10-15 05:15:37

COMPLETED

2021-10-15

731

2021-10-15

2021-11-03

63,113,904

n: 400

On va préparer quelques pré-calculs et deux fonctions de résumé.

Code
subject_counts <- table(ex_adsl$ARM)

countpercentid <- function(num, ARM) {
  n <- length(unique(num))
  if (n == 0) {
    pct <- 0
  } else {
    pct <- 100*n/subject_counts[ARM[1]]
  }
  sprintf("%d (%.2f%%)", length(unique(num)), pct)
}

count <- function(x) sprintf("(N=%d)", length(x))

On peut enfin exprimer le tableau de suivi des évènements indésirables avec ‘tables’.

Code
library(tables)
body <- tabular( 
  Heading("Patients with at least one event")*1*
    Heading("")*countpercentid*Arguments(ARM = ARM)*
    Heading()*USUBJID +
    Heading("Total number of events")*1*Heading("")*1 +
    Heading()*AEBODSYS*
    (Heading("Patients with at least one event")*
       Percent(denom = ARM, fn = countpercentid)*
       Heading()*USUBJID +
       Heading("Total number of events")*1 +
       Heading()*AEDECOD*DropEmpty(which = "row")*
       Heading()*Percent(denom = ARM, fn = countpercentid)*
       Heading()*USUBJID) ~ 
    Heading()*ARM, 
data = ex_adae )
Code
body
A: Drug X B: Placebo C: Combination
Patients with at least one event 122 (91.04%) 123 (91.79%) 120 (90.91%)
Total number of events 609 622 703
cl A.1 Patients with at least one event 78 (58.21%) 75 (55.97%) 89 (66.42%)
  Total number of events 132 130 160
  dcd A.1.1.1.1 50 (37.31%) 45 (33.58%) 63 (47.01%)
  dcd A.1.1.1.2 48 (35.82%) 48 (35.82%) 50 (37.31%)
cl B.1 Patients with at least one event 47 (35.07%) 49 (36.57%) 43 (32.09%)
  Total number of events 56 60 62
  dcd B.1.1.1.1 47 (35.07%) 49 (36.57%) 43 (32.09%)
cl B.2 Patients with at least one event 79 (58.96%) 74 (55.22%) 85 (63.43%)
  Total number of events 129 138 143
  dcd B.2.1.2.1 49 (36.57%) 44 (32.84%) 52 (38.81%)
  dcd B.2.2.3.1 48 (35.82%) 54 (40.30%) 51 (38.06%)
cl C.1 Patients with at least one event 43 (32.09%) 46 (34.33%) 43 (32.09%)
  Total number of events 55 63 64
  dcd C.1.1.1.3 43 (32.09%) 46 (34.33%) 43 (32.09%)
cl C.2 Patients with at least one event 35 (26.12%) 48 (35.82%) 55 (41.04%)
  Total number of events 48 53 65
  dcd C.2.1.2.1 35 (26.12%) 48 (35.82%) 55 (41.04%)
cl D.1 Patients with at least one event 79 (58.96%) 67 (50.00%) 80 (59.70%)
  Total number of events 127 106 135
  dcd D.1.1.1.1 50 (37.31%) 42 (31.34%) 51 (38.06%)
  dcd D.1.1.4.2 48 (35.82%) 42 (31.34%) 50 (37.31%)
cl D.2 Patients with at least one event 47 (35.07%) 58 (43.28%) 57 (42.54%)
  Total number of events 62 72 74
  dcd D.2.1.5.3 47 (35.07%) 58 (43.28%) 57 (42.54%)

5.3 La fonction as_flextable

Code
library(flextable)
as_flextable(body, spread_first_col = TRUE, add_tab = TRUE) |>
  align(j = 1, part = "all", align = "left") |> 
  padding(padding = 4, part = "all") |> 
  add_header_row(
    values = c("", fmt_header_n(subject_counts, newline = FALSE)),
    top = FALSE) |> 
  hline(i = 1, part = "header", border = fp_border_default(width = 0))

A: Drug X

B: Placebo

C: Combination

(N=134)

(N=134)

(N=132)

Patients with at least one event

122 (91.04%)

123 (91.79%)

120 (90.91%)

Total number of events

609

622

703

cl A.1

Patients with at least one event

78 (58.21%)

75 (55.97%)

89 (66.42%)

Total number of events

132

130

160

dcd A.1.1.1.1

50 (37.31%)

45 (33.58%)

63 (47.01%)

dcd A.1.1.1.2

48 (35.82%)

48 (35.82%)

50 (37.31%)

cl B.1

Patients with at least one event

47 (35.07%)

49 (36.57%)

43 (32.09%)

Total number of events

56

60

62

dcd B.1.1.1.1

47 (35.07%)

49 (36.57%)

43 (32.09%)

cl B.2

Patients with at least one event

79 (58.96%)

74 (55.22%)

85 (63.43%)

Total number of events

129

138

143

dcd B.2.1.2.1

49 (36.57%)

44 (32.84%)

52 (38.81%)

dcd B.2.2.3.1

48 (35.82%)

54 (40.30%)

51 (38.06%)

cl C.1

Patients with at least one event

43 (32.09%)

46 (34.33%)

43 (32.09%)

Total number of events

55

63

64

dcd C.1.1.1.3

43 (32.09%)

46 (34.33%)

43 (32.09%)

cl C.2

Patients with at least one event

35 (26.12%)

48 (35.82%)

55 (41.04%)

Total number of events

48

53

65

dcd C.2.1.2.1

35 (26.12%)

48 (35.82%)

55 (41.04%)

cl D.1

Patients with at least one event

79 (58.96%)

67 (50.00%)

80 (59.70%)

Total number of events

127

106

135

dcd D.1.1.1.1

50 (37.31%)

42 (31.34%)

51 (38.06%)

dcd D.1.1.4.2

48 (35.82%)

42 (31.34%)

50 (37.31%)

cl D.2

Patients with at least one event

47 (35.07%)

58 (43.28%)

57 (42.54%)

Total number of events

62

72

74

dcd D.2.1.5.3

47 (35.07%)

58 (43.28%)

57 (42.54%)