@@ -57,3 +57,112 @@ lyt <- basic_table(show_colcounts = TRUE) %>%
5757result <- build_table(lyt , df = adae , alt_counts_df = adsl )
5858
5959result
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