Skip to content

Commit 1222a9d

Browse files
authored
Merge pull request #133 from pharmaverse/tfrmt_example
closes #122 add Tfrmt demographic and ae table examples
2 parents 5246712 + a9858e2 commit 1222a9d

File tree

6 files changed

+478
-5
lines changed

6 files changed

+478
-5
lines changed

DESCRIPTION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Authors@R:
1616
person("Aksel", "Thomsen", , "oath@novonordisk.com", role = "aut"),
1717
person("Shiyu", "Chen", , "shiyu.chen@atorusresearch.com", role = "aut"),
1818
person("Rammprasad", "Ganapathy", , "rammprasad.ganapathy@gene.com", role = "aut")
19+
person("Alanah", "Jonas", , "alanah.x.jonas@gsk.com", role = "aut"),
1920
Description: This is not a package, but we just use this file to declare
2021
the dependencies of the site.
2122
URL: https://github.com/pharmaverse/examples
@@ -26,6 +27,7 @@ Imports:
2627
cards,
2728
dplyr,
2829
filters,
30+
forcats,
2931
gtsummary,
3032
lubridate,
3133
labelled,
@@ -50,6 +52,7 @@ Imports:
5052
teal.modules.clinical,
5153
teal.modules.general,
5254
tern,
55+
tfrmt,
5356
tidyr,
5457
xportr,
5558
logr,

inst/WORDLIST

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ae
4141
AE
4242
AEACN
4343
AEBDSYCD
44+
aebodsys
4445
AEBODSYS
4546
AEDECOD
4647
AEDIS
@@ -84,6 +85,7 @@ AESTDAT
8485
AESTDTC
8586
AESTDY
8687
AETERM
88+
aeterm
8789
AETHNIC
8890
AETHNICN
8991
AFRLT
@@ -164,6 +166,7 @@ bds
164166
BDS
165167
BILI
166168
BILIBL
169+
bigN
167170
BLAS
168171
blq
169172
BLQ
@@ -351,14 +354,19 @@ fansi
351354
FAS
352355
FASFL
353356
fastmap
357+
fct
354358
FDA's
355359
fileext
356360
flextable
357361
flx
358362
fmt
363+
fns
364+
forcats
359365
formatters
360366
FORMN
367+
freqs
361368
FRLTU
369+
frmt
362370
FRVDT
363371
fs
364372
Garolini
@@ -388,6 +396,7 @@ iconv
388396
ifelse
389397
iframe
390398
init
399+
inorder
391400
inputId
392401
installedpackages
393402
ITT
@@ -494,6 +503,7 @@ onco
494503
ONCO
495504
ontrtfl
496505
ONTRTFL
506+
ord
497507
os
498508
othgrpvars
499509
outfile
@@ -532,6 +542,7 @@ PCTEST
532542
PCTESTCD
533543
PCTPT
534544
PCTPTNUM
545+
pcts
535546
pfs
536547
PFS
537548
pharma
@@ -604,6 +615,7 @@ refdates
604615
refdt
605616
REGIONCAT
606617
REGIONx
618+
relevel
607619
renderPlot
608620
renderUI
609621
renv
@@ -713,6 +725,7 @@ tempfile
713725
TEMPLOC
714726
TESTCD
715727
tf
728+
tfrmt
716729
tformat
717730
tgdt
718731
tgt

tlg/adverse_events.R

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,112 @@ lyt <- basic_table(show_colcounts = TRUE) %>%
5757
result <- build_table(lyt, df = adae, alt_counts_df = adsl)
5858

5959
result
60+
61+
## ----r message=FALSE, warning=FALSE-------------------------------------------
62+
library(pharmaverseadam) # for clinical trial data
63+
library(dplyr) # for data manipulation
64+
library(cards) # for creating analysis result displays
65+
library(tfrmt) # for formatting tables in R
66+
67+
## ----r------------------------------------------------------------------------
68+
# Filter to include only subjects marked as part of the safety population
69+
adsl <- pharmaverseadam::adsl |>
70+
filter(SAFFL == "Y")
71+
# Load adverse event data
72+
adae <- pharmaverseadam::adae |>
73+
filter(SAFFL == "Y" & TRTEMFL == "Y")
74+
75+
## ----r------------------------------------------------------------------------
76+
# Create an ARD that stacks hierarchical data of adverse events
77+
# Grouping by treatment, system organ class, and preferred term
78+
ae_ard <- ard_stack_hierarchical(
79+
data = adae,
80+
by = TRT01A, # Note: by variables must be present in the denominator dataset
81+
variables = c(AEBODSYS, AETERM),
82+
statistic = ~ c("n", "p"), # Calculate count and percentage
83+
denominator = adsl,
84+
id = USUBJID,
85+
over_variables = TRUE,
86+
overall = TRUE
87+
)
88+
89+
# Filter adae and adsl with trt01a set to "Total" and create a new ARD for the total column
90+
adae2 <- adae |>
91+
mutate(TRT01A = "Total")
92+
adsl2 <- adsl |>
93+
mutate(TRT01A = "Total")
94+
95+
ae2_ard <- ard_stack_hierarchical(
96+
data = adae2,
97+
by = TRT01A, # Note: by variables must be present in the denominator dataset
98+
variables = c(AEBODSYS, AETERM),
99+
denominator = adsl2,
100+
statistic = ~ c("n", "p"),
101+
id = USUBJID,
102+
over_variables = TRUE,
103+
overall = TRUE
104+
) |>
105+
filter(group2 == "TRT01A" | variable == "TRT01A") # filter to stats we need
106+
107+
## ----r------------------------------------------------------------------------
108+
ae3_ard <- bind_ard(ae_ard, ae2_ard) |>
109+
# reshape the data
110+
shuffle_card(fill_hierarchical_overall = "ANY EVENT") |>
111+
# transform group-level freqs/pcts into a singular "bigN" row
112+
prep_big_n(vars = "TRT01A") |>
113+
# for nested variables, fill any missing values with "ANY EVENT"
114+
prep_hierarchical_fill(vars = c("AEBODSYS", "AETERM"), fill = "ANY EVENT") |>
115+
mutate(TRT01A = ifelse(TRT01A == "Overall TRT01A", "Total", TRT01A))
116+
117+
# create ordering columns, sort by AEBODSYS
118+
ordering_aebodsys <- ae3_ard |>
119+
filter(TRT01A == "Total", stat_name == "n", AETERM == "ANY EVENT") |>
120+
arrange(desc(stat)) |>
121+
mutate(ord1 = row_number()) |>
122+
select(AEBODSYS, ord1)
123+
124+
# sort by AETERM after AEBODSYS order
125+
ordering_aeterm <- ae3_ard |>
126+
filter(TRT01A == "Total", stat_name == "n") |>
127+
group_by(AEBODSYS) |>
128+
arrange(desc(stat)) |>
129+
mutate(ord2 = row_number()) |>
130+
select(AEBODSYS, AETERM, ord2)
131+
132+
# join on our ordering columns and keep required columns
133+
ae4_ard <- ae3_ard |>
134+
full_join(ordering_aebodsys, by = "AEBODSYS") |>
135+
full_join(ordering_aeterm, by = c("AEBODSYS", "AETERM")) |>
136+
select(AEBODSYS, AETERM, ord1, ord2, stat, stat_name, TRT01A)
137+
138+
## ----r------------------------------------------------------------------------
139+
AE_T01 <- tfrmt_n_pct(
140+
n = "n", pct = "p",
141+
pct_frmt_when = frmt_when(
142+
"==1" ~ frmt("(100%)"),
143+
">=0.995" ~ frmt("(>99%)"),
144+
"==0" ~ frmt(""),
145+
"<=0.01" ~ frmt("(<1%)"),
146+
"TRUE" ~ frmt("(xx.x%)", transform = ~ . * 100)
147+
)
148+
) |>
149+
tfrmt(
150+
group = AEBODSYS,
151+
label = AETERM,
152+
param = stat_name,
153+
value = stat,
154+
column = TRT01A,
155+
sorting_cols = c(ord1, ord2),
156+
col_plan = col_plan(
157+
"System Organ Class
158+
Preferred Term" = AEBODSYS, Placebo, `Xanomeline High Dose`, `Xanomeline Low Dose`,
159+
-ord1, -ord2
160+
),
161+
row_grp_plan = row_grp_plan(row_grp_structure(
162+
group_val = ".default", element_block(post_space = " ")
163+
)),
164+
big_n = big_n_structure(param_val = "bigN", n_frmt = frmt(" (N=xx)"))
165+
) |>
166+
print_to_gt(ae4_ard)
167+
168+
AE_T01

0 commit comments

Comments
 (0)