Skip to content
Merged
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Authors@R:
person("Aksel", "Thomsen", , "[email protected]", role = "aut"),
person("Shiyu", "Chen", , "[email protected]", role = "aut"),
person("Rammprasad", "Ganapathy", , "[email protected]", role = "aut")
person("Alanah", "Jonas", , "[email protected]", role = "aut"),
Description: This is not a package, but we just use this file to declare
the dependencies of the site.
URL: https://github.com/pharmaverse/examples
Expand All @@ -26,6 +27,7 @@ Imports:
cards,
dplyr,
filters,
forcats,
gtsummary,
lubridate,
labelled,
Expand All @@ -50,6 +52,7 @@ Imports:
teal.modules.clinical,
teal.modules.general,
tern,
tfrmt,
tidyr,
xportr,
logr,
Expand Down
13 changes: 13 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ae
AE
AEACN
AEBDSYCD
aebodsys
AEBODSYS
AEDECOD
AEDIS
Expand Down Expand Up @@ -84,6 +85,7 @@ AESTDAT
AESTDTC
AESTDY
AETERM
aeterm
AETHNIC
AETHNICN
AFRLT
Expand Down Expand Up @@ -164,6 +166,7 @@ bds
BDS
BILI
BILIBL
bigN
BLAS
blq
BLQ
Expand Down Expand Up @@ -351,14 +354,19 @@ fansi
FAS
FASFL
fastmap
fct
FDA's
fileext
flextable
flx
fmt
fns
forcats
formatters
FORMN
freqs
FRLTU
frmt
FRVDT
fs
Garolini
Expand Down Expand Up @@ -388,6 +396,7 @@ iconv
ifelse
iframe
init
inorder
inputId
installedpackages
ITT
Expand Down Expand Up @@ -494,6 +503,7 @@ onco
ONCO
ontrtfl
ONTRTFL
ord
os
othgrpvars
outfile
Expand Down Expand Up @@ -532,6 +542,7 @@ PCTEST
PCTESTCD
PCTPT
PCTPTNUM
pcts
pfs
PFS
pharma
Expand Down Expand Up @@ -604,6 +615,7 @@ refdates
refdt
REGIONCAT
REGIONx
relevel
renderPlot
renderUI
renv
Expand Down Expand Up @@ -713,6 +725,7 @@ tempfile
TEMPLOC
TESTCD
tf
tfrmt
tformat
tgdt
tgt
Expand Down
109 changes: 109 additions & 0 deletions tlg/adverse_events.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,112 @@ lyt <- basic_table(show_colcounts = TRUE) %>%
result <- build_table(lyt, df = adae, alt_counts_df = adsl)

result

## ----r message=FALSE, warning=FALSE-------------------------------------------
library(pharmaverseadam) # for clinical trial data
library(dplyr) # for data manipulation
library(cards) # for creating analysis result displays
library(tfrmt) # for formatting tables in R

## ----r------------------------------------------------------------------------
# Filter to include only subjects marked as part of the safety population
adsl <- pharmaverseadam::adsl |>
filter(SAFFL == "Y")
# Load adverse event data
adae <- pharmaverseadam::adae |>
filter(SAFFL == "Y" & TRTEMFL == "Y")

## ----r------------------------------------------------------------------------
# Create an ARD that stacks hierarchical data of adverse events
# Grouping by treatment, system organ class, and preferred term
ae_ard <- ard_stack_hierarchical(
data = adae,
by = TRT01A, # Note: by variables must be present in the denominator dataset
variables = c(AEBODSYS, AETERM),
statistic = ~ c("n", "p"), # Calculate count and percentage
denominator = adsl,
id = USUBJID,
over_variables = TRUE,
overall = TRUE
)

# Filter adae and adsl with trt01a set to "Total" and create a new ARD for the total column
adae2 <- adae |>
mutate(TRT01A = "Total")
adsl2 <- adsl |>
mutate(TRT01A = "Total")

ae2_ard <- ard_stack_hierarchical(
data = adae2,
by = TRT01A, # Note: by variables must be present in the denominator dataset
variables = c(AEBODSYS, AETERM),
denominator = adsl2,
statistic = ~ c("n", "p"),
id = USUBJID,
over_variables = TRUE,
overall = TRUE
) |>
filter(group2 == "TRT01A" | variable == "TRT01A") # filter to stats we need

## ----r------------------------------------------------------------------------
ae3_ard <- bind_ard(ae_ard, ae2_ard) |>
# reshape the data
shuffle_card(fill_hierarchical_overall = "ANY EVENT") |>
# transform group-level freqs/pcts into a singular "bigN" row
prep_big_n(vars = "TRT01A") |>
# for nested variables, fill any missing values with "ANY EVENT"
prep_hierarchical_fill(vars = c("AEBODSYS", "AETERM"), fill = "ANY EVENT") |>
mutate(TRT01A = ifelse(TRT01A == "Overall TRT01A", "Total", TRT01A))

# create ordering columns, sort by AEBODSYS
ordering_aebodsys <- ae3_ard |>
filter(TRT01A == "Total", stat_name == "n", AETERM == "ANY EVENT") |>
arrange(desc(stat)) |>
mutate(ord1 = row_number()) |>
select(AEBODSYS, ord1)

# sort by AETERM after AEBODSYS order
ordering_aeterm <- ae3_ard |>
filter(TRT01A == "Total", stat_name == "n") |>
group_by(AEBODSYS) |>
arrange(desc(stat)) |>
mutate(ord2 = row_number()) |>
select(AEBODSYS, AETERM, ord2)

# join on our ordering columns and keep required columns
ae4_ard <- ae3_ard |>
full_join(ordering_aebodsys, by = "AEBODSYS") |>
full_join(ordering_aeterm, by = c("AEBODSYS", "AETERM")) |>
select(AEBODSYS, AETERM, ord1, ord2, stat, stat_name, TRT01A)

## ----r------------------------------------------------------------------------
AE_T01 <- tfrmt_n_pct(
n = "n", pct = "p",
pct_frmt_when = frmt_when(
"==1" ~ frmt("(100%)"),
">=0.995" ~ frmt("(>99%)"),
"==0" ~ frmt(""),
"<=0.01" ~ frmt("(<1%)"),
"TRUE" ~ frmt("(xx.x%)", transform = ~ . * 100)
)
) |>
tfrmt(
group = AEBODSYS,
label = AETERM,
param = stat_name,
value = stat,
column = TRT01A,
sorting_cols = c(ord1, ord2),
col_plan = col_plan(
"System Organ Class
Preferred Term" = AEBODSYS, Placebo, `Xanomeline High Dose`, `Xanomeline Low Dose`,
-ord1, -ord2
),
row_grp_plan = row_grp_plan(row_grp_structure(
group_val = ".default", element_block(post_space = " ")
)),
big_n = big_n_structure(param_val = "bigN", n_frmt = frmt(" (N=xx)"))
) |>
print_to_gt(ae4_ard)

AE_T01
Loading