pharmaverse examples
  1. TLG
  2. Adverse Events
  • Introduction

  • SDTM
    • DM
    • VS
    • AE
  • ADaM
    • ADSL
    • ADPC
    • ADPPK
    • ADRS
    • ADTTE
    • ADVS
    • ADAE
  • TLG
    • Demographic Table
    • Adverse Events
    • Pharmacokinetic
  • Documents
    • Slides
    • Documents
  • Interactive
    • teal applications
  • Logs
    • The Difference Between logr, logrx, and whirl
  • eSubmission
    • eSubmission

  • Session Info
  • Pharmaverse Home
  1. TLG
  2. Adverse Events

Adverse Events

Introduction

This guide will show you how pharmaverse packages, along with some from tidyverse, can be used to create an Adverse Events table, using the {pharmaverseadam} ADSL and ADAE data as an input. ## Multiple Approaches Shown

This example includes:

  • ARD-based approach: {cards} + {tfrmt} - Creates structured Analysis Results Datasets following CDISC standards, then formats them into tables
  • Classic approach: {rtables} + {tern} - Direct table creation from datasets using mature, stable packages

In the examples below, we illustrate two general approaches for creating an Adverse events table. The first is the classic method of creating summary tables directly from a data set. The second utilizes Analysis Results Datasets—part of the emerging CDISC Analysis Results Standard.

{rtables} & {tern}

The packages used with a brief description of their purpose are as follows:

  • {rtables}: designed to create and display complex tables with R.
  • {tern}: contains analysis functions to create tables and graphs used for clinical trial reporting.

Load Data and Required pharmaverse Package

After installation of packages, the first step is to load our pharmaverse packages and input data. Here, we are going to encode missing entries in a data frame adsl and adae.

Note that {tern} depends on {rtables} so the latter is automatically attached.

library(pharmaverseadam)
library(tern)
library(dplyr)

adsl <- adsl %>%
  df_explicit_na()

adae <- adae %>%
  df_explicit_na()

Start preprocessing

Now we will add some pre-processing to add labels ready for display in the table and how the output will be split.

adae <- adae %>%
  var_relabel(
    AEBODSYS = "MedDRA System Organ Class",
    AEDECOD = "MedDRA Preferred Term"
  ) %>%
  filter(SAFFL == "Y")

# Define the split function
split_fun <- drop_split_levels

Adverse Events table

Now we create the Adverse Events table.

lyt <- basic_table(show_colcounts = TRUE) %>%
  split_cols_by(var = "ACTARM") %>%
  add_overall_col(label = "All Patients") %>%
  analyze_num_patients(
    vars = "USUBJID",
    .stats = c("unique", "nonunique"),
    .labels = c(
      unique = "Total number of patients with at least one adverse event",
      nonunique = "Overall total number of events"
    )
  ) %>%
  split_rows_by(
    "AEBODSYS",
    child_labels = "visible",
    nested = FALSE,
    split_fun = split_fun,
    label_pos = "topleft",
    split_label = obj_label(adae$AEBODSYS)
  ) %>%
  summarize_num_patients(
    var = "USUBJID",
    .stats = c("unique", "nonunique"),
    .labels = c(
      unique = "Total number of patients with at least one adverse event",
      nonunique = "Total number of events"
    )
  ) %>%
  count_occurrences(
    vars = "AEDECOD",
    .indent_mods = -1L
  ) %>%
  append_varlabels(adae, "AEDECOD", indent = 1L)

result <- build_table(lyt, df = adae, alt_counts_df = adsl)

result
MedDRA System Organ Class                                              Placebo     Xanomeline High Dose   Xanomeline Low Dose   All Patients
  MedDRA Preferred Term                                                 (N=86)            (N=72)                (N=96)            (N=306)   
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
Total number of patients with at least one adverse event              69 (80.2%)        70 (97.2%)            86 (89.6%)        225 (73.5%) 
Overall total number of events                                           301               436                    454               1191    
CARDIAC DISORDERS                                                                                                                           
  Total number of patients with at least one adverse event            13 (15.1%)        15 (20.8%)            16 (16.7%)         44 (14.4%) 
  Total number of events                                                  27                30                    34                 91     
  ATRIAL FIBRILLATION                                                  1 (1.2%)          2 (2.8%)              2 (2.1%)           5 (1.6%)  
  ATRIAL FLUTTER                                                          0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  ATRIAL HYPERTROPHY                                                   1 (1.2%)             0                      0              1 (0.3%)  
  ATRIOVENTRICULAR BLOCK FIRST DEGREE                                  1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  ATRIOVENTRICULAR BLOCK SECOND DEGREE                                 2 (2.3%)          1 (1.4%)              2 (2.1%)           5 (1.6%)  
  BRADYCARDIA                                                          1 (1.2%)             0                      0              1 (0.3%)  
  BUNDLE BRANCH BLOCK LEFT                                             1 (1.2%)             0                      0              1 (0.3%)  
  BUNDLE BRANCH BLOCK RIGHT                                            1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  CARDIAC DISORDER                                                        0              1 (1.4%)                  0              1 (0.3%)  
  CARDIAC FAILURE CONGESTIVE                                           1 (1.2%)             0                      0              1 (0.3%)  
  MYOCARDIAL INFARCTION                                                4 (4.7%)          4 (5.6%)              2 (2.1%)          10 (3.3%)  
  PALPITATIONS                                                            0                 0                  2 (2.1%)           2 (0.7%)  
  SINUS ARRHYTHMIA                                                     1 (1.2%)             0                      0              1 (0.3%)  
  SINUS BRADYCARDIA                                                    2 (2.3%)         8 (11.1%)              7 (7.3%)          17 (5.6%)  
  SUPRAVENTRICULAR EXTRASYSTOLES                                       1 (1.2%)          1 (1.4%)              1 (1.0%)           3 (1.0%)  
  SUPRAVENTRICULAR TACHYCARDIA                                            0                 0                  1 (1.0%)           1 (0.3%)  
  TACHYCARDIA                                                          1 (1.2%)             0                      0              1 (0.3%)  
  VENTRICULAR EXTRASYSTOLES                                               0              1 (1.4%)              2 (2.1%)           3 (1.0%)  
  VENTRICULAR HYPERTROPHY                                              1 (1.2%)             0                      0              1 (0.3%)  
  WOLFF-PARKINSON-WHITE SYNDROME                                          0                 0                  1 (1.0%)           1 (0.3%)  
CONGENITAL, FAMILIAL AND GENETIC DISORDERS                                                                                                  
  Total number of patients with at least one adverse event                0              2 (2.8%)              1 (1.0%)           3 (1.0%)  
  Total number of events                                                  0                 2                      1                 3      
  VENTRICULAR SEPTAL DEFECT                                               0              2 (2.8%)              1 (1.0%)           3 (1.0%)  
EAR AND LABYRINTH DISORDERS                                                                                                                 
  Total number of patients with at least one adverse event             1 (1.2%)          1 (1.4%)              2 (2.1%)           4 (1.3%)  
  Total number of events                                                  2                 1                      3                 6      
  CERUMEN IMPACTION                                                       0                 0                  1 (1.0%)           1 (0.3%)  
  EAR PAIN                                                             1 (1.2%)             0                      0              1 (0.3%)  
  TINNITUS                                                                0                 0                  1 (1.0%)           1 (0.3%)  
  VERTIGO                                                                 0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
EYE DISORDERS                                                                                                                               
  Total number of patients with at least one adverse event             4 (4.7%)          1 (1.4%)              2 (2.1%)           7 (2.3%)  
  Total number of events                                                  8                 2                      2                 12     
  CONJUNCTIVAL HAEMORRHAGE                                                0                 0                  1 (1.0%)           1 (0.3%)  
  CONJUNCTIVITIS                                                       2 (2.3%)             0                      0              2 (0.7%)  
  EYE ALLERGY                                                          1 (1.2%)             0                      0              1 (0.3%)  
  EYE PRURITUS                                                         1 (1.2%)             0                      0              1 (0.3%)  
  EYE SWELLING                                                         1 (1.2%)             0                      0              1 (0.3%)  
  GLAUCOMA                                                             1 (1.2%)             0                      0              1 (0.3%)  
  VISION BLURRED                                                          0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
GASTROINTESTINAL DISORDERS                                                                                                                  
  Total number of patients with at least one adverse event            17 (19.8%)        20 (27.8%)            16 (16.7%)         53 (17.3%) 
  Total number of events                                                  26                35                    26                 87     
  ABDOMINAL DISCOMFORT                                                    0              1 (1.4%)                  0              1 (0.3%)  
  ABDOMINAL PAIN                                                       1 (1.2%)          1 (1.4%)              3 (3.1%)           5 (1.6%)  
  CONSTIPATION                                                         1 (1.2%)             0                      0              1 (0.3%)  
  DIARRHOEA                                                           9 (10.5%)          3 (4.2%)              6 (6.2%)          18 (5.9%)  
  DYSPEPSIA                                                            1 (1.2%)          1 (1.4%)              1 (1.0%)           3 (1.0%)  
  DYSPHAGIA                                                               0                 0                  1 (1.0%)           1 (0.3%)  
  FLATULENCE                                                           1 (1.2%)             0                      0              1 (0.3%)  
  GASTROINTESTINAL HAEMORRHAGE                                            0              1 (1.4%)                  0              1 (0.3%)  
  GASTROOESOPHAGEAL REFLUX DISEASE                                     1 (1.2%)             0                      0              1 (0.3%)  
  GLOSSITIS                                                            1 (1.2%)             0                      0              1 (0.3%)  
  HIATUS HERNIA                                                        1 (1.2%)             0                      0              1 (0.3%)  
  NAUSEA                                                               3 (3.5%)          6 (8.3%)              3 (3.1%)          12 (3.9%)  
  RECTAL HAEMORRHAGE                                                      0                 0                  1 (1.0%)           1 (0.3%)  
  SALIVARY HYPERSECRETION                                                 0              4 (5.6%)                  0              4 (1.3%)  
  STOMACH DISCOMFORT                                                      0              1 (1.4%)                  0              1 (0.3%)  
  VOMITING                                                             3 (3.5%)          6 (8.3%)              4 (4.2%)          13 (4.2%)  
GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS                                                                                        
  Total number of patients with at least one adverse event            21 (24.4%)        36 (50.0%)            51 (53.1%)        108 (35.3%) 
  Total number of events                                                  48               118                    126               292     
  APPLICATION SITE BLEEDING                                               0                 0                  1 (1.0%)           1 (0.3%)  
  APPLICATION SITE DERMATITIS                                          5 (5.8%)          7 (9.7%)              9 (9.4%)          21 (6.9%)  
  APPLICATION SITE DESQUAMATION                                           0                 0                  1 (1.0%)           1 (0.3%)  
  APPLICATION SITE DISCHARGE                                              0              1 (1.4%)                  0              1 (0.3%)  
  APPLICATION SITE DISCOLOURATION                                         0                 0                  1 (1.0%)           1 (0.3%)  
  APPLICATION SITE ERYTHEMA                                            3 (3.5%)         14 (19.4%)            13 (13.5%)         30 (9.8%)  
  APPLICATION SITE INDURATION                                          1 (1.2%)             0                      0              1 (0.3%)  
  APPLICATION SITE IRRITATION                                          3 (3.5%)         9 (12.5%)              9 (9.4%)          21 (6.9%)  
  APPLICATION SITE PAIN                                                   0              2 (2.8%)                  0              2 (0.7%)  
  APPLICATION SITE PERSPIRATION                                           0              2 (2.8%)                  0              2 (0.7%)  
  APPLICATION SITE PRURITUS                                            6 (7.0%)         21 (29.2%)            23 (24.0%)         50 (16.3%) 
  APPLICATION SITE REACTION                                            1 (1.2%)          1 (1.4%)                  0              2 (0.7%)  
  APPLICATION SITE SWELLING                                               0              2 (2.8%)              1 (1.0%)           3 (1.0%)  
  APPLICATION SITE URTICARIA                                              0              1 (1.4%)              2 (2.1%)           3 (1.0%)  
  APPLICATION SITE VESICLES                                            1 (1.2%)          5 (6.9%)              5 (5.2%)          11 (3.6%)  
  APPLICATION SITE WARMTH                                                 0                 0                  1 (1.0%)           1 (0.3%)  
  ASTHENIA                                                             1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  CHEST DISCOMFORT                                                        0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  CHEST PAIN                                                              0              2 (2.8%)                  0              2 (0.7%)  
  CHILLS                                                               1 (1.2%)          1 (1.4%)              1 (1.0%)           3 (1.0%)  
  CYST                                                                    0                 0                  1 (1.0%)           1 (0.3%)  
  FATIGUE                                                              1 (1.2%)          5 (6.9%)              5 (5.2%)          11 (3.6%)  
  FEELING ABNORMAL                                                        0              1 (1.4%)                  0              1 (0.3%)  
  FEELING COLD                                                            0              1 (1.4%)                  0              1 (0.3%)  
  INFLAMMATION                                                            0                 0                  1 (1.0%)           1 (0.3%)  
  MALAISE                                                                 0              2 (2.8%)              1 (1.0%)           3 (1.0%)  
  OEDEMA                                                                  0                 0                  2 (2.1%)           2 (0.7%)  
  OEDEMA PERIPHERAL                                                    2 (2.3%)          2 (2.8%)              1 (1.0%)           5 (1.6%)  
  PAIN                                                                    0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  PYREXIA                                                              2 (2.3%)             0                  1 (1.0%)           3 (1.0%)  
  SECRETION DISCHARGE                                                     0                 0                  1 (1.0%)           1 (0.3%)  
  SUDDEN DEATH                                                            0                 0                  1 (1.0%)           1 (0.3%)  
  SWELLING                                                                0                 0                  1 (1.0%)           1 (0.3%)  
  ULCER                                                                   0                 0                  1 (1.0%)           1 (0.3%)  
HEPATOBILIARY DISORDERS                                                                                                                     
  Total number of patients with at least one adverse event             1 (1.2%)             0                      0              1 (0.3%)  
  Total number of events                                                  1                 0                      0                 1      
  HYPERBILIRUBINAEMIA                                                  1 (1.2%)             0                      0              1 (0.3%)  
IMMUNE SYSTEM DISORDERS                                                                                                                     
  Total number of patients with at least one adverse event                0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  Total number of events                                                  0                 1                      2                 3      
  HYPERSENSITIVITY                                                        0                 0                  1 (1.0%)           1 (0.3%)  
  SEASONAL ALLERGY                                                        0              1 (1.4%)                  0              1 (0.3%)  
INFECTIONS AND INFESTATIONS                                                                                                                 
  Total number of patients with at least one adverse event            16 (18.6%)        13 (18.1%)            10 (10.4%)         39 (12.7%) 
  Total number of events                                                  35                20                    18                 73     
  BRONCHITIS                                                           1 (1.2%)             0                      0              1 (0.3%)  
  CELLULITIS                                                              0                 0                  1 (1.0%)           1 (0.3%)  
  CERVICITIS                                                           1 (1.2%)             0                      0              1 (0.3%)  
  CYSTITIS                                                             1 (1.2%)          1 (1.4%)                  0              2 (0.7%)  
  EAR INFECTION                                                        2 (2.3%)             0                      0              2 (0.7%)  
  GASTROENTERITIS VIRAL                                                1 (1.2%)             0                      0              1 (0.3%)  
  HORDEOLUM                                                               0              1 (1.4%)                  0              1 (0.3%)  
  INFLUENZA                                                            1 (1.2%)          1 (1.4%)              1 (1.0%)           3 (1.0%)  
  LOCALISED INFECTION                                                  1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  LOWER RESPIRATORY TRACT INFECTION                                       0              1 (1.4%)                  0              1 (0.3%)  
  NASOPHARYNGITIS                                                      2 (2.3%)          6 (8.3%)              4 (4.2%)          12 (3.9%)  
  ONYCHOMYCOSIS                                                           0                 0                  1 (1.0%)           1 (0.3%)  
  PNEUMONIA                                                               0                 0                  1 (1.0%)           1 (0.3%)  
  RHINITIS                                                                0              1 (1.4%)                  0              1 (0.3%)  
  UPPER RESPIRATORY TRACT INFECTION                                    6 (7.0%)          3 (4.2%)              1 (1.0%)          10 (3.3%)  
  URINARY TRACT INFECTION                                              2 (2.3%)          1 (1.4%)                  0              3 (1.0%)  
  VAGINAL MYCOSIS                                                      1 (1.2%)             0                      0              1 (0.3%)  
  VIRAL INFECTION                                                         0                 0                  1 (1.0%)           1 (0.3%)  
INJURY, POISONING AND PROCEDURAL COMPLICATIONS                                                                                              
  Total number of patients with at least one adverse event             4 (4.7%)          5 (6.9%)              5 (5.2%)          14 (4.6%)  
  Total number of events                                                  9                 8                     12                 29     
  CONTUSION                                                            1 (1.2%)          2 (2.8%)              1 (1.0%)           4 (1.3%)  
  EXCORIATION                                                          2 (2.3%)          1 (1.4%)              1 (1.0%)           4 (1.3%)  
  FACIAL BONES FRACTURE                                                   0              1 (1.4%)                  0              1 (0.3%)  
  FALL                                                                 1 (1.2%)          1 (1.4%)              2 (2.1%)           4 (1.3%)  
  HIP FRACTURE                                                         1 (1.2%)          2 (2.8%)                  0              3 (1.0%)  
  JOINT DISLOCATION                                                       0                 0                  1 (1.0%)           1 (0.3%)  
  SKIN LACERATION                                                      1 (1.2%)             0                  2 (2.1%)           3 (1.0%)  
  WOUND                                                                   0                 0                  1 (1.0%)           1 (0.3%)  
INVESTIGATIONS                                                                                                                              
  Total number of patients with at least one adverse event            10 (11.6%)         5 (6.9%)              8 (8.3%)          23 (7.5%)  
  Total number of events                                                  19                6                     15                 40     
  BIOPSY                                                                  0              1 (1.4%)                  0              1 (0.3%)  
  BIOPSY PROSTATE                                                         0              1 (1.4%)                  0              1 (0.3%)  
  BLOOD ALKALINE PHOSPHATASE INCREASED                                 1 (1.2%)             0                      0              1 (0.3%)  
  BLOOD CHOLESTEROL INCREASED                                             0              1 (1.4%)                  0              1 (0.3%)  
  BLOOD CREATINE PHOSPHOKINASE INCREASED                               1 (1.2%)             0                      0              1 (0.3%)  
  BLOOD GLUCOSE INCREASED                                                 0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  BLOOD URINE PRESENT                                                  1 (1.2%)             0                      0              1 (0.3%)  
  BODY TEMPERATURE INCREASED                                              0                 0                  1 (1.0%)           1 (0.3%)  
  CYSTOSCOPY                                                           1 (1.2%)             0                      0              1 (0.3%)  
  ELECTROCARDIOGRAM ST SEGMENT DEPRESSION                              4 (4.7%)             0                  1 (1.0%)           5 (1.6%)  
  ELECTROCARDIOGRAM T WAVE AMPLITUDE DECREASED                         1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  ELECTROCARDIOGRAM T WAVE INVERSION                                   2 (2.3%)          1 (1.4%)              1 (1.0%)           4 (1.3%)  
  HEART RATE INCREASED                                                 1 (1.2%)             0                      0              1 (0.3%)  
  HEART RATE IRREGULAR                                                 1 (1.2%)             0                      0              1 (0.3%)  
  NASAL MUCOSA BIOPSY                                                     0                 0                  1 (1.0%)           1 (0.3%)  
  NEUTROPHIL COUNT INCREASED                                              0                 0                  1 (1.0%)           1 (0.3%)  
  URINE ANALYSIS ABNORMAL                                                 0                 0                  1 (1.0%)           1 (0.3%)  
  WEIGHT DECREASED                                                        0                 0                  1 (1.0%)           1 (0.3%)  
  WHITE BLOOD CELL COUNT INCREASED                                        0                 0                  1 (1.0%)           1 (0.3%)  
METABOLISM AND NUTRITION DISORDERS                                                                                                          
  Total number of patients with at least one adverse event             6 (7.0%)          3 (4.2%)              1 (1.0%)          10 (3.3%)  
  Total number of events                                                  8                 5                      1                 14     
  DECREASED APPETITE                                                   1 (1.2%)          1 (1.4%)                  0              2 (0.7%)  
  DEHYDRATION                                                          1 (1.2%)             0                      0              1 (0.3%)  
  DIABETES MELLITUS                                                    1 (1.2%)             0                      0              1 (0.3%)  
  FOOD CRAVING                                                         1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  HYPERCHOLESTEROLAEMIA                                                   0              1 (1.4%)                  0              1 (0.3%)  
  HYPONATRAEMIA                                                        1 (1.2%)             0                      0              1 (0.3%)  
  INCREASED APPETITE                                                   1 (1.2%)          1 (1.4%)                  0              2 (0.7%)  
MUSCULOSKELETAL AND CONNECTIVE TISSUE DISORDERS                                                                                             
  Total number of patients with at least one adverse event             5 (5.8%)         8 (11.1%)              7 (7.3%)          20 (6.5%)  
  Total number of events                                                  8                 11                    10                 29     
  ARTHRALGIA                                                           1 (1.2%)          1 (1.4%)              2 (2.1%)           4 (1.3%)  
  ARTHRITIS                                                            1 (1.2%)          1 (1.4%)                  0              2 (0.7%)  
  BACK PAIN                                                            1 (1.2%)          3 (4.2%)              1 (1.0%)           5 (1.6%)  
  FLANK PAIN                                                              0              2 (2.8%)                  0              2 (0.7%)  
  MUSCLE SPASMS                                                           0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  MUSCULAR WEAKNESS                                                       0                 0                  1 (1.0%)           1 (0.3%)  
  MYALGIA                                                                 0              1 (1.4%)                  0              1 (0.3%)  
  PAIN IN EXTREMITY                                                    1 (1.2%)             0                      0              1 (0.3%)  
  SHOULDER PAIN                                                        1 (1.2%)             0                  2 (2.1%)           3 (1.0%)  
NEOPLASMS BENIGN, MALIGNANT AND UNSPECIFIED (INCL CYSTS AND POLYPS)                                                                         
  Total number of patients with at least one adverse event                0              1 (1.4%)              2 (2.1%)           3 (1.0%)  
  Total number of events                                                  0                 1                      3                 4      
  COLON CANCER                                                            0                 0                  1 (1.0%)           1 (0.3%)  
  MALIGNANT FIBROUS HISTIOCYTOMA                                          0                 0                  1 (1.0%)           1 (0.3%)  
  PROSTATE CANCER                                                         0              1 (1.4%)                  0              1 (0.3%)  
NERVOUS SYSTEM DISORDERS                                                                                                                    
  Total number of patients with at least one adverse event            12 (14.0%)        25 (34.7%)            22 (22.9%)         59 (19.3%) 
  Total number of events                                                  16                43                    42                101     
  AMNESIA                                                                 0              1 (1.4%)                  0              1 (0.3%)  
  BALANCE DISORDER                                                        0                 0                  1 (1.0%)           1 (0.3%)  
  BURNING SENSATION                                                       0              2 (2.8%)                  0              2 (0.7%)  
  COGNITIVE DISORDER                                                      0              1 (1.4%)                  0              1 (0.3%)  
  COMPLEX PARTIAL SEIZURES                                                0                 0                  1 (1.0%)           1 (0.3%)  
  COORDINATION ABNORMAL                                                   0                 0                  1 (1.0%)           1 (0.3%)  
  DIZZINESS                                                            2 (2.3%)         11 (15.3%)             9 (9.4%)          22 (7.2%)  
  HEADACHE                                                             7 (8.1%)          6 (8.3%)              3 (3.1%)          16 (5.2%)  
  HEMIANOPIA HOMONYMOUS                                                   0                 0                  1 (1.0%)           1 (0.3%)  
  HYPERSOMNIA                                                             0              1 (1.4%)                  0              1 (0.3%)  
  LETHARGY                                                                0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  PARAESTHESIA                                                            0              1 (1.4%)                  0              1 (0.3%)  
  PARAESTHESIA ORAL                                                       0                 0                  1 (1.0%)           1 (0.3%)  
  PARKINSON'S DISEASE                                                  1 (1.2%)             0                      0              1 (0.3%)  
  PAROSMIA                                                                0              1 (1.4%)                  0              1 (0.3%)  
  PARTIAL SEIZURES WITH SECONDARY GENERALISATION                          0              1 (1.4%)                  0              1 (0.3%)  
  PSYCHOMOTOR HYPERACTIVITY                                            1 (1.2%)             0                      0              1 (0.3%)  
  SOMNOLENCE                                                           2 (2.3%)          1 (1.4%)              3 (3.1%)           6 (2.0%)  
  STUPOR                                                                  0                 0                  1 (1.0%)           1 (0.3%)  
  SYNCOPE                                                                 0              2 (2.8%)              5 (5.2%)           7 (2.3%)  
  SYNCOPE VASOVAGAL                                                       0              1 (1.4%)                  0              1 (0.3%)  
  TRANSIENT ISCHAEMIC ATTACK                                              0              1 (1.4%)              2 (2.1%)           3 (1.0%)  
PSYCHIATRIC DISORDERS                                                                                                                       
  Total number of patients with at least one adverse event            10 (11.6%)        8 (11.1%)             11 (11.5%)         29 (9.5%)  
  Total number of events                                                  14                11                    15                 40     
  AGITATION                                                            2 (2.3%)             0                  3 (3.1%)           5 (1.6%)  
  ANXIETY                                                              1 (1.2%)             0                  3 (3.1%)           4 (1.3%)  
  COMPLETED SUICIDE                                                    1 (1.2%)             0                      0              1 (0.3%)  
  CONFUSIONAL STATE                                                    2 (2.3%)          1 (1.4%)              3 (3.1%)           6 (2.0%)  
  DELIRIUM                                                                0              1 (1.4%)                  0              1 (0.3%)  
  DELUSION                                                             1 (1.2%)          1 (1.4%)                  0              2 (0.7%)  
  DEPRESSED MOOD                                                          0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  DISORIENTATION                                                       1 (1.2%)             0                      0              1 (0.3%)  
  HALLUCINATION                                                           0              1 (1.4%)                  0              1 (0.3%)  
  HALLUCINATION, VISUAL                                                   0              1 (1.4%)                  0              1 (0.3%)  
  INSOMNIA                                                             2 (2.3%)          2 (2.8%)                  0              4 (1.3%)  
  IRRITABILITY                                                         1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  LIBIDO DECREASED                                                        0              1 (1.4%)                  0              1 (0.3%)  
  LISTLESS                                                                0              1 (1.4%)                  0              1 (0.3%)  
  NIGHTMARE                                                               0              1 (1.4%)                  0              1 (0.3%)  
  RESTLESSNESS                                                            0                 0                  1 (1.0%)           1 (0.3%)  
RENAL AND URINARY DISORDERS                                                                                                                 
  Total number of patients with at least one adverse event             4 (4.7%)          3 (4.2%)              4 (4.2%)          11 (3.6%)  
  Total number of events                                                  5                 4                      4                 13     
  CALCULUS URETHRAL                                                       0              1 (1.4%)                  0              1 (0.3%)  
  DYSURIA                                                              1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  ENURESIS                                                                0                 0                  1 (1.0%)           1 (0.3%)  
  INCONTINENCE                                                            0                 0                  1 (1.0%)           1 (0.3%)  
  MICTURITION URGENCY                                                  1 (1.2%)          1 (1.4%)              1 (1.0%)           3 (1.0%)  
  NEPHROLITHIASIS                                                      1 (1.2%)          1 (1.4%)                  0              2 (0.7%)  
  POLLAKIURIA                                                          1 (1.2%)             0                      0              1 (0.3%)  
REPRODUCTIVE SYSTEM AND BREAST DISORDERS                                                                                                    
  Total number of patients with at least one adverse event             2 (2.3%)          1 (1.4%)                  0              3 (1.0%)  
  Total number of events                                                  4                 1                      0                 5      
  BENIGN PROSTATIC HYPERPLASIA                                         1 (1.2%)          1 (1.4%)                  0              2 (0.7%)  
  PELVIC PAIN                                                          1 (1.2%)             0                      0              1 (0.3%)  
RESPIRATORY, THORACIC AND MEDIASTINAL DISORDERS                                                                                             
  Total number of patients with at least one adverse event            10 (11.6%)        10 (13.9%)            10 (10.4%)         30 (9.8%)  
  Total number of events                                                  15                22                    16                 53     
  ALLERGIC GRANULOMATOUS ANGIITIS                                         0              1 (1.4%)                  0              1 (0.3%)  
  COUGH                                                                3 (3.5%)          5 (6.9%)              6 (6.2%)          14 (4.6%)  
  DYSPHONIA                                                               0                 0                  1 (1.0%)           1 (0.3%)  
  DYSPNOEA                                                             1 (1.2%)          1 (1.4%)              1 (1.0%)           3 (1.0%)  
  EMPHYSEMA                                                            1 (1.2%)             0                      0              1 (0.3%)  
  EPISTAXIS                                                               0              2 (2.8%)              1 (1.0%)           3 (1.0%)  
  HAEMOPTYSIS                                                          1 (1.2%)             0                      0              1 (0.3%)  
  NASAL CONGESTION                                                     3 (3.5%)          3 (4.2%)              1 (1.0%)           7 (2.3%)  
  PHARYNGEAL ERYTHEMA                                                     0              1 (1.4%)                  0              1 (0.3%)  
  PHARYNGOLARYNGEAL PAIN                                                  0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  POSTNASAL DRIP                                                       1 (1.2%)             0                      0              1 (0.3%)  
  PRODUCTIVE COUGH                                                        0              1 (1.4%)                  0              1 (0.3%)  
  RALES                                                                1 (1.2%)             0                      0              1 (0.3%)  
  RESPIRATORY TRACT CONGESTION                                            0              1 (1.4%)                  0              1 (0.3%)  
  RHINORRHOEA                                                             0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
SKIN AND SUBCUTANEOUS TISSUE DISORDERS                                                                                                      
  Total number of patients with at least one adverse event            21 (24.4%)        42 (58.3%)            42 (43.8%)        105 (34.3%) 
  Total number of events                                                  47               111                    118               276     
  ACTINIC KERATOSIS                                                       0              1 (1.4%)                  0              1 (0.3%)  
  ALOPECIA                                                             1 (1.2%)             0                      0              1 (0.3%)  
  BLISTER                                                                 0              1 (1.4%)              5 (5.2%)           6 (2.0%)  
  COLD SWEAT                                                           1 (1.2%)             0                      0              1 (0.3%)  
  DERMATITIS ATOPIC                                                    1 (1.2%)             0                      0              1 (0.3%)  
  DERMATITIS CONTACT                                                      0                 0                  1 (1.0%)           1 (0.3%)  
  DRUG ERUPTION                                                        1 (1.2%)             0                      0              1 (0.3%)  
  ERYTHEMA                                                            9 (10.5%)         14 (19.4%)            15 (15.6%)         38 (12.4%) 
  HYPERHIDROSIS                                                        2 (2.3%)         8 (11.1%)              4 (4.2%)          14 (4.6%)  
  PRURITUS                                                             8 (9.3%)         26 (36.1%)            23 (24.0%)         57 (18.6%) 
  PRURITUS GENERALISED                                                    0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
  RASH                                                                 5 (5.8%)         11 (15.3%)            13 (13.5%)         29 (9.5%)  
  RASH ERYTHEMATOUS                                                       0                 0                  2 (2.1%)           2 (0.7%)  
  RASH MACULO-PAPULAR                                                     0              1 (1.4%)                  0              1 (0.3%)  
  RASH PAPULAR                                                            0              1 (1.4%)                  0              1 (0.3%)  
  RASH PRURITIC                                                           0              2 (2.8%)              1 (1.0%)           3 (1.0%)  
  SKIN EXFOLIATION                                                        0                 0                  1 (1.0%)           1 (0.3%)  
  SKIN IRRITATION                                                      3 (3.5%)          5 (6.9%)              6 (6.2%)          14 (4.6%)  
  SKIN ODOUR ABNORMAL                                                     0              1 (1.4%)                  0              1 (0.3%)  
  SKIN ULCER                                                           1 (1.2%)             0                      0              1 (0.3%)  
  URTICARIA                                                               0              1 (1.4%)              1 (1.0%)           2 (0.7%)  
SOCIAL CIRCUMSTANCES                                                                                                                        
  Total number of patients with at least one adverse event                0              1 (1.4%)                  0              1 (0.3%)  
  Total number of events                                                  0                 1                      0                 1      
  ALCOHOL USE                                                             0              1 (1.4%)                  0              1 (0.3%)  
SURGICAL AND MEDICAL PROCEDURES                                                                                                             
  Total number of patients with at least one adverse event             2 (2.3%)          2 (2.8%)              1 (1.0%)           5 (1.6%)  
  Total number of events                                                  2                 2                      1                 5      
  ACROCHORDON EXCISION                                                    0              1 (1.4%)                  0              1 (0.3%)  
  CATARACT OPERATION                                                   1 (1.2%)             0                  1 (1.0%)           2 (0.7%)  
  EYE LASER SURGERY                                                    1 (1.2%)             0                      0              1 (0.3%)  
  SKIN LESION EXCISION                                                    0              1 (1.4%)                  0              1 (0.3%)  
VASCULAR DISORDERS                                                                                                                          
  Total number of patients with at least one adverse event             3 (3.5%)          1 (1.4%)              4 (4.2%)           8 (2.6%)  
  Total number of events                                                  7                 1                      5                 13     
  HOT FLUSH                                                               0                 0                  1 (1.0%)           1 (0.3%)  
  HYPERTENSION                                                         1 (1.2%)             0                  2 (2.1%)           3 (1.0%)  
  HYPOTENSION                                                          2 (2.3%)             0                  1 (1.0%)           3 (1.0%)  
  ORTHOSTATIC HYPOTENSION                                              1 (1.2%)             0                      0              1 (0.3%)  
  WOUND HAEMORRHAGE                                                       0              1 (1.4%)                  0              1 (0.3%)  

{tfrmt} & {cards}

In the example below, we will use the {tfrmt} and {cards} packages to create a adverse events tables.

  • The {cards} package creates Analysis Results Datasets (ARDs, which are a part of the CDISC Analysis Results Standard).
  • The {tfrmt} utilizes ARDs to create tables.

In the example below, we first build an ARD with the needed summary statistics using {cards}. Then, we use the ARD to build the adverse events table with {tfrmt}.

Load libraries

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

Step 1: Import data

Subset to safety population.

# 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")

Step 2: Data Preparation

Analysis

Create an Analysis Result Display (ARD) using the {cards} package.

# 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

Tidy for table

Further refine the ARD for use in tfrmt

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)

Step 3: Creating the {tfrmt} Table

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
System Organ Class Preferred Term Placebo (N=86) Xanomeline High Dose (N=72) Xanomeline Low Dose (N=96) Total (N=254)
ANY EVENT 65 (75.6%) 68 (94.4%) 84 (87.5%) 217 (85.4%)
           
GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS



  ANY EVENT 21 (24.4%) 36 (50.0%) 51 (53.1%) 108 (42.5%)
  APPLICATION SITE PRURITUS 6 ( 7.0%) 21 (29.2%) 23 (24.0%) 50 (19.7%)
  APPLICATION SITE ERYTHEMA 3 ( 3.5%) 14 (19.4%) 13 (13.5%) 30 (11.8%)
  APPLICATION SITE DERMATITIS 5 ( 5.8%) 7 ( 9.7%) 9 ( 9.4%) 21 ( 8.3%)
  APPLICATION SITE IRRITATION 3 ( 3.5%) 9 (12.5%) 9 ( 9.4%) 21 ( 8.3%)
  APPLICATION SITE VESICLES 1 ( 1.2%) 5 ( 6.9%) 5 ( 5.2%) 11 ( 4.3%)
  FATIGUE 1 ( 1.2%) 5 ( 6.9%) 5 ( 5.2%) 11 ( 4.3%)
  OEDEMA PERIPHERAL 2 ( 2.3%) 2 ( 2.8%) 1 ( 1.0%) 5 ( 2.0%)
  APPLICATION SITE SWELLING 0  2 ( 2.8%) 1 ( 1.0%) 3 ( 1.2%)
  APPLICATION SITE URTICARIA 0  1 ( 1.4%) 2 ( 2.1%) 3 ( 1.2%)
  CHILLS 1 ( 1.2%) 1 ( 1.4%) 1 ( 1.0%) 3 ( 1.2%)
  MALAISE 0  2 ( 2.8%) 1 ( 1.0%) 3 ( 1.2%)
  PYREXIA 2 ( 2.3%) 0  1 ( 1.0%) 3 ( 1.2%)
  APPLICATION SITE PAIN 0  2 ( 2.8%) 0  2 (<1%)
  APPLICATION SITE PERSPIRATION 0  2 ( 2.8%) 0  2 (<1%)
  APPLICATION SITE REACTION 1 ( 1.2%) 1 ( 1.4%) 0  2 (<1%)
  ASTHENIA 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  CHEST DISCOMFORT 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  CHEST PAIN 0  2 ( 2.8%) 0  2 (<1%)
  OEDEMA 0  0  2 ( 2.1%) 2 (<1%)
  PAIN 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  APPLICATION SITE BLEEDING 0  0  1 ( 1.0%) 1 (<1%)
  APPLICATION SITE DESQUAMATION 0  0  1 ( 1.0%) 1 (<1%)
  APPLICATION SITE DISCHARGE 0  1 ( 1.4%) 0  1 (<1%)
  APPLICATION SITE DISCOLOURATION 0  0  1 ( 1.0%) 1 (<1%)
  APPLICATION SITE INDURATION 1 ( 1.2%) 0  0  1 (<1%)
  APPLICATION SITE WARMTH 0  0  1 ( 1.0%) 1 (<1%)
  FEELING ABNORMAL 0  1 ( 1.4%) 0  1 (<1%)
  FEELING COLD 0  1 ( 1.4%) 0  1 (<1%)
  INFLAMMATION 0  0  1 ( 1.0%) 1 (<1%)
  SECRETION DISCHARGE 0  0  1 ( 1.0%) 1 (<1%)
  SUDDEN DEATH 0  0  1 ( 1.0%) 1 (<1%)
  SWELLING 0  0  1 ( 1.0%) 1 (<1%)
  ULCER 0  0  1 ( 1.0%) 1 (<1%)
           
SKIN AND SUBCUTANEOUS TISSUE DISORDERS



  ANY EVENT 20 (23.3%) 39 (54.2%) 39 (40.6%) 98 (38.6%)
  PRURITUS 8 ( 9.3%) 25 (34.7%) 21 (21.9%) 54 (21.3%)
  ERYTHEMA 8 ( 9.3%) 14 (19.4%) 14 (14.6%) 36 (14.2%)
  RASH 5 ( 5.8%) 8 (11.1%) 13 (13.5%) 26 (10.2%)
  HYPERHIDROSIS 2 ( 2.3%) 8 (11.1%) 4 ( 4.2%) 14 ( 5.5%)
  SKIN IRRITATION 3 ( 3.5%) 5 ( 6.9%) 6 ( 6.2%) 14 ( 5.5%)
  BLISTER 0  1 ( 1.4%) 5 ( 5.2%) 6 ( 2.4%)
  RASH PRURITIC 0  2 ( 2.8%) 1 ( 1.0%) 3 ( 1.2%)
  PRURITUS GENERALISED 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  URTICARIA 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  ACTINIC KERATOSIS 0  1 ( 1.4%) 0  1 (<1%)
  ALOPECIA 1 ( 1.2%) 0  0  1 (<1%)
  COLD SWEAT 1 ( 1.2%) 0  0  1 (<1%)
  DERMATITIS CONTACT 0  0  1 ( 1.0%) 1 (<1%)
  DRUG ERUPTION 1 ( 1.2%) 0  0  1 (<1%)
  RASH ERYTHEMATOUS 0  0  1 ( 1.0%) 1 (<1%)
  RASH MACULO-PAPULAR 0  1 ( 1.4%) 0  1 (<1%)
  SKIN EXFOLIATION 0  0  1 ( 1.0%) 1 (<1%)
  SKIN ODOUR ABNORMAL 0  1 ( 1.4%) 0  1 (<1%)
  SKIN ULCER 1 ( 1.2%) 0  0  1 (<1%)
           
NERVOUS SYSTEM DISORDERS



  ANY EVENT 8 ( 9.3%) 23 (31.9%) 22 (22.9%) 53 (20.9%)
  DIZZINESS 2 ( 2.3%) 10 (13.9%) 9 ( 9.4%) 21 ( 8.3%)
  HEADACHE 3 ( 3.5%) 5 ( 6.9%) 3 ( 3.1%) 11 ( 4.3%)
  SYNCOPE 0  2 ( 2.8%) 5 ( 5.2%) 7 ( 2.8%)
  SOMNOLENCE 2 ( 2.3%) 1 ( 1.4%) 3 ( 3.1%) 6 ( 2.4%)
  TRANSIENT ISCHAEMIC ATTACK 0  1 ( 1.4%) 2 ( 2.1%) 3 ( 1.2%)
  BURNING SENSATION 0  2 ( 2.8%) 0  2 (<1%)
  LETHARGY 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  AMNESIA 0  1 ( 1.4%) 0  1 (<1%)
  BALANCE DISORDER 0  0  1 ( 1.0%) 1 (<1%)
  COGNITIVE DISORDER 0  1 ( 1.4%) 0  1 (<1%)
  COMPLEX PARTIAL SEIZURES 0  0  1 ( 1.0%) 1 (<1%)
  COORDINATION ABNORMAL 0  0  1 ( 1.0%) 1 (<1%)
  HEMIANOPIA HOMONYMOUS 0  0  1 ( 1.0%) 1 (<1%)
  HYPERSOMNIA 0  1 ( 1.4%) 0  1 (<1%)
  PARAESTHESIA 0  1 ( 1.4%) 0  1 (<1%)
  PARAESTHESIA ORAL 0  0  1 ( 1.0%) 1 (<1%)
  PARKINSON'S DISEASE 1 ( 1.2%) 0  0  1 (<1%)
  PAROSMIA 0  1 ( 1.4%) 0  1 (<1%)
  PARTIAL SEIZURES WITH SECONDARY GENERALISATION 0  1 ( 1.4%) 0  1 (<1%)
  PSYCHOMOTOR HYPERACTIVITY 1 ( 1.2%) 0  0  1 (<1%)
  STUPOR 0  0  1 ( 1.0%) 1 (<1%)
  SYNCOPE VASOVAGAL 0  1 ( 1.4%) 0  1 (<1%)
           
GASTROINTESTINAL DISORDERS



  ANY EVENT 17 (19.8%) 19 (26.4%) 15 (15.6%) 51 (20.1%)
  DIARRHOEA 9 (10.5%) 3 ( 4.2%) 5 ( 5.2%) 17 ( 6.7%)
  VOMITING 3 ( 3.5%) 6 ( 8.3%) 4 ( 4.2%) 13 ( 5.1%)
  NAUSEA 3 ( 3.5%) 6 ( 8.3%) 3 ( 3.1%) 12 ( 4.7%)
  ABDOMINAL PAIN 1 ( 1.2%) 1 ( 1.4%) 3 ( 3.1%) 5 ( 2.0%)
  SALIVARY HYPERSECRETION 0  4 ( 5.6%) 0  4 ( 1.6%)
  DYSPEPSIA 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  ABDOMINAL DISCOMFORT 0  1 ( 1.4%) 0  1 (<1%)
  CONSTIPATION 1 ( 1.2%) 0  0  1 (<1%)
  DYSPHAGIA 0  0  1 ( 1.0%) 1 (<1%)
  FLATULENCE 1 ( 1.2%) 0  0  1 (<1%)
  GASTROINTESTINAL HAEMORRHAGE 0  1 ( 1.4%) 0  1 (<1%)
  GASTROOESOPHAGEAL REFLUX DISEASE 1 ( 1.2%) 0  0  1 (<1%)
  GLOSSITIS 1 ( 1.2%) 0  0  1 (<1%)
  HIATUS HERNIA 1 ( 1.2%) 0  0  1 (<1%)
  RECTAL HAEMORRHAGE 0  0  1 ( 1.0%) 1 (<1%)
  STOMACH DISCOMFORT 0  1 ( 1.4%) 0  1 (<1%)
           
CARDIAC DISORDERS



  ANY EVENT 12 (14.0%) 14 (19.4%) 14 (14.6%) 40 (15.7%)
  SINUS BRADYCARDIA 2 ( 2.3%) 8 (11.1%) 7 ( 7.3%) 17 ( 6.7%)
  MYOCARDIAL INFARCTION 4 ( 4.7%) 4 ( 5.6%) 2 ( 2.1%) 10 ( 3.9%)
  ATRIAL FIBRILLATION 1 ( 1.2%) 2 ( 2.8%) 2 ( 2.1%) 5 ( 2.0%)
  SUPRAVENTRICULAR EXTRASYSTOLES 1 ( 1.2%) 1 ( 1.4%) 1 ( 1.0%) 3 ( 1.2%)
  VENTRICULAR EXTRASYSTOLES 0  1 ( 1.4%) 2 ( 2.1%) 3 ( 1.2%)
  ATRIAL FLUTTER 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  ATRIOVENTRICULAR BLOCK FIRST DEGREE 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  BUNDLE BRANCH BLOCK RIGHT 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  PALPITATIONS 0  0  2 ( 2.1%) 2 (<1%)
  ATRIAL HYPERTROPHY 1 ( 1.2%) 0  0  1 (<1%)
  ATRIOVENTRICULAR BLOCK SECOND DEGREE 1 ( 1.2%) 0  0  1 (<1%)
  BRADYCARDIA 1 ( 1.2%) 0  0  1 (<1%)
  BUNDLE BRANCH BLOCK LEFT 1 ( 1.2%) 0  0  1 (<1%)
  CARDIAC DISORDER 0  1 ( 1.4%) 0  1 (<1%)
  CARDIAC FAILURE CONGESTIVE 1 ( 1.2%) 0  0  1 (<1%)
  SINUS ARRHYTHMIA 1 ( 1.2%) 0  0  1 (<1%)
  SUPRAVENTRICULAR TACHYCARDIA 0  0  1 ( 1.0%) 1 (<1%)
  TACHYCARDIA 1 ( 1.2%) 0  0  1 (<1%)
  VENTRICULAR HYPERTROPHY 1 ( 1.2%) 0  0  1 (<1%)
  WOLFF-PARKINSON-WHITE SYNDROME 0  0  1 ( 1.0%) 1 (<1%)
           
INFECTIONS AND INFESTATIONS



  ANY EVENT 16 (18.6%) 13 (18.1%) 9 ( 9.4%) 38 (15.0%)
  NASOPHARYNGITIS 2 ( 2.3%) 6 ( 8.3%) 4 ( 4.2%) 12 ( 4.7%)
  UPPER RESPIRATORY TRACT INFECTION 6 ( 7.0%) 3 ( 4.2%) 1 ( 1.0%) 10 ( 3.9%)
  INFLUENZA 1 ( 1.2%) 1 ( 1.4%) 1 ( 1.0%) 3 ( 1.2%)
  URINARY TRACT INFECTION 2 ( 2.3%) 1 ( 1.4%) 0  3 ( 1.2%)
  CYSTITIS 1 ( 1.2%) 1 ( 1.4%) 0  2 (<1%)
  EAR INFECTION 2 ( 2.3%) 0  0  2 (<1%)
  BRONCHITIS 1 ( 1.2%) 0  0  1 (<1%)
  CELLULITIS 0  0  1 ( 1.0%) 1 (<1%)
  CERVICITIS 1 ( 1.2%) 0  0  1 (<1%)
  GASTROENTERITIS VIRAL 1 ( 1.2%) 0  0  1 (<1%)
  HORDEOLUM 0  1 ( 1.4%) 0  1 (<1%)
  LOCALISED INFECTION 1 ( 1.2%) 0  0  1 (<1%)
  LOWER RESPIRATORY TRACT INFECTION 0  1 ( 1.4%) 0  1 (<1%)
  PNEUMONIA 0  0  1 ( 1.0%) 1 (<1%)
  RHINITIS 0  1 ( 1.4%) 0  1 (<1%)
  VAGINAL MYCOSIS 1 ( 1.2%) 0  0  1 (<1%)
  VIRAL INFECTION 0  0  1 ( 1.0%) 1 (<1%)
           
PSYCHIATRIC DISORDERS



  ANY EVENT 10 (11.6%) 7 ( 9.7%) 11 (11.5%) 28 (11.0%)
  CONFUSIONAL STATE 2 ( 2.3%) 1 ( 1.4%) 3 ( 3.1%) 6 ( 2.4%)
  AGITATION 2 ( 2.3%) 0  3 ( 3.1%) 5 ( 2.0%)
  INSOMNIA 2 ( 2.3%) 2 ( 2.8%) 0  4 ( 1.6%)
  ANXIETY 0  0  3 ( 3.1%) 3 ( 1.2%)
  DELUSION 1 ( 1.2%) 1 ( 1.4%) 0  2 (<1%)
  IRRITABILITY 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  COMPLETED SUICIDE 1 ( 1.2%) 0  0  1 (<1%)
  DELIRIUM 0  1 ( 1.4%) 0  1 (<1%)
  DEPRESSED MOOD 0  0  1 ( 1.0%) 1 (<1%)
  DISORIENTATION 1 ( 1.2%) 0  0  1 (<1%)
  HALLUCINATION 0  1 ( 1.4%) 0  1 (<1%)
  HALLUCINATION, VISUAL 0  1 ( 1.4%) 0  1 (<1%)
  LIBIDO DECREASED 0  1 ( 1.4%) 0  1 (<1%)
  LISTLESS 0  1 ( 1.4%) 0  1 (<1%)
  NIGHTMARE 0  1 ( 1.4%) 0  1 (<1%)
  RESTLESSNESS 0  0  1 ( 1.0%) 1 (<1%)
           
RESPIRATORY, THORACIC AND MEDIASTINAL DISORDERS



  ANY EVENT 8 ( 9.3%) 10 (13.9%) 9 ( 9.4%) 27 (10.6%)
  COUGH 1 ( 1.2%) 5 ( 6.9%) 5 ( 5.2%) 11 ( 4.3%)
  NASAL CONGESTION 3 ( 3.5%) 3 ( 4.2%) 1 ( 1.0%) 7 ( 2.8%)
  DYSPNOEA 1 ( 1.2%) 1 ( 1.4%) 1 ( 1.0%) 3 ( 1.2%)
  EPISTAXIS 0  2 ( 2.8%) 1 ( 1.0%) 3 ( 1.2%)
  PHARYNGOLARYNGEAL PAIN 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  RHINORRHOEA 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  ALLERGIC GRANULOMATOUS ANGIITIS 0  1 ( 1.4%) 0  1 (<1%)
  DYSPHONIA 0  0  1 ( 1.0%) 1 (<1%)
  EMPHYSEMA 1 ( 1.2%) 0  0  1 (<1%)
  HAEMOPTYSIS 1 ( 1.2%) 0  0  1 (<1%)
  PHARYNGEAL ERYTHEMA 0  1 ( 1.4%) 0  1 (<1%)
  POSTNASAL DRIP 1 ( 1.2%) 0  0  1 (<1%)
  PRODUCTIVE COUGH 0  1 ( 1.4%) 0  1 (<1%)
  RALES 1 ( 1.2%) 0  0  1 (<1%)
  RESPIRATORY TRACT CONGESTION 0  1 ( 1.4%) 0  1 (<1%)
           
INVESTIGATIONS



  ANY EVENT 10 (11.6%) 5 ( 6.9%) 7 ( 7.3%) 22 ( 8.7%)
  ELECTROCARDIOGRAM ST SEGMENT DEPRESSION 4 ( 4.7%) 0  1 ( 1.0%) 5 ( 2.0%)
  ELECTROCARDIOGRAM T WAVE INVERSION 2 ( 2.3%) 1 ( 1.4%) 1 ( 1.0%) 4 ( 1.6%)
  BLOOD GLUCOSE INCREASED 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  ELECTROCARDIOGRAM T WAVE AMPLITUDE DECREASED 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  BIOPSY 0  1 ( 1.4%) 0  1 (<1%)
  BIOPSY PROSTATE 0  1 ( 1.4%) 0  1 (<1%)
  BLOOD ALKALINE PHOSPHATASE INCREASED 1 ( 1.2%) 0  0  1 (<1%)
  BLOOD CHOLESTEROL INCREASED 0  1 ( 1.4%) 0  1 (<1%)
  BLOOD CREATINE PHOSPHOKINASE INCREASED 1 ( 1.2%) 0  0  1 (<1%)
  BLOOD URINE PRESENT 1 ( 1.2%) 0  0  1 (<1%)
  BODY TEMPERATURE INCREASED 0  0  1 ( 1.0%) 1 (<1%)
  CYSTOSCOPY 1 ( 1.2%) 0  0  1 (<1%)
  HEART RATE INCREASED 1 ( 1.2%) 0  0  1 (<1%)
  HEART RATE IRREGULAR 1 ( 1.2%) 0  0  1 (<1%)
  NASAL MUCOSA BIOPSY 0  0  1 ( 1.0%) 1 (<1%)
  WEIGHT DECREASED 0  0  1 ( 1.0%) 1 (<1%)
           
MUSCULOSKELETAL AND CONNECTIVE TISSUE DISORDERS



  ANY EVENT 4 ( 4.7%) 7 ( 9.7%) 7 ( 7.3%) 18 ( 7.1%)
  BACK PAIN 1 ( 1.2%) 3 ( 4.2%) 1 ( 1.0%) 5 ( 2.0%)
  ARTHRALGIA 1 ( 1.2%) 1 ( 1.4%) 2 ( 2.1%) 4 ( 1.6%)
  SHOULDER PAIN 1 ( 1.2%) 0  2 ( 2.1%) 3 ( 1.2%)
  MUSCLE SPASMS 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  ARTHRITIS 0  1 ( 1.4%) 0  1 (<1%)
  FLANK PAIN 0  1 ( 1.4%) 0  1 (<1%)
  MUSCULAR WEAKNESS 0  0  1 ( 1.0%) 1 (<1%)
  MYALGIA 0  1 ( 1.4%) 0  1 (<1%)
  PAIN IN EXTREMITY 1 ( 1.2%) 0  0  1 (<1%)
           
INJURY, POISONING AND PROCEDURAL COMPLICATIONS



  ANY EVENT 4 ( 4.7%) 5 ( 6.9%) 5 ( 5.2%) 14 ( 5.5%)
  CONTUSION 1 ( 1.2%) 2 ( 2.8%) 1 ( 1.0%) 4 ( 1.6%)
  EXCORIATION 2 ( 2.3%) 1 ( 1.4%) 1 ( 1.0%) 4 ( 1.6%)
  FALL 1 ( 1.2%) 1 ( 1.4%) 2 ( 2.1%) 4 ( 1.6%)
  HIP FRACTURE 1 ( 1.2%) 2 ( 2.8%) 0  3 ( 1.2%)
  SKIN LACERATION 1 ( 1.2%) 0  2 ( 2.1%) 3 ( 1.2%)
  FACIAL BONES FRACTURE 0  1 ( 1.4%) 0  1 (<1%)
  JOINT DISLOCATION 0  0  1 ( 1.0%) 1 (<1%)
  WOUND 0  0  1 ( 1.0%) 1 (<1%)
           
RENAL AND URINARY DISORDERS



  ANY EVENT 4 ( 4.7%) 3 ( 4.2%) 3 ( 3.1%) 10 ( 3.9%)
  MICTURITION URGENCY 1 ( 1.2%) 1 ( 1.4%) 1 ( 1.0%) 3 ( 1.2%)
  DYSURIA 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  NEPHROLITHIASIS 1 ( 1.2%) 1 ( 1.4%) 0  2 (<1%)
  CALCULUS URETHRAL 0  1 ( 1.4%) 0  1 (<1%)
  INCONTINENCE 0  0  1 ( 1.0%) 1 (<1%)
  POLLAKIURIA 1 ( 1.2%) 0  0  1 (<1%)
           
METABOLISM AND NUTRITION DISORDERS



  ANY EVENT 6 ( 7.0%) 2 ( 2.8%) 1 ( 1.0%) 9 ( 3.5%)
  DECREASED APPETITE 1 ( 1.2%) 1 ( 1.4%) 0  2 (<1%)
  FOOD CRAVING 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  INCREASED APPETITE 1 ( 1.2%) 1 ( 1.4%) 0  2 (<1%)
  DEHYDRATION 1 ( 1.2%) 0  0  1 (<1%)
  DIABETES MELLITUS 1 ( 1.2%) 0  0  1 (<1%)
  HYPONATRAEMIA 1 ( 1.2%) 0  0  1 (<1%)
           
VASCULAR DISORDERS



  ANY EVENT 3 ( 3.5%) 1 ( 1.4%) 3 ( 3.1%) 7 ( 2.8%)
  HYPOTENSION 2 ( 2.3%) 0  1 ( 1.0%) 3 ( 1.2%)
  HYPERTENSION 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  HOT FLUSH 0  0  1 ( 1.0%) 1 (<1%)
  ORTHOSTATIC HYPOTENSION 1 ( 1.2%) 0  0  1 (<1%)
  WOUND HAEMORRHAGE 0  1 ( 1.4%) 0  1 (<1%)
           
EYE DISORDERS



  ANY EVENT 2 ( 2.3%) 1 ( 1.4%) 2 ( 2.1%) 5 ( 2.0%)
  VISION BLURRED 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  CONJUNCTIVAL HAEMORRHAGE 0  0  1 ( 1.0%) 1 (<1%)
  CONJUNCTIVITIS 1 ( 1.2%) 0  0  1 (<1%)
  EYE ALLERGY 1 ( 1.2%) 0  0  1 (<1%)
  EYE PRURITUS 1 ( 1.2%) 0  0  1 (<1%)
  EYE SWELLING 1 ( 1.2%) 0  0  1 (<1%)
           
SURGICAL AND MEDICAL PROCEDURES



  ANY EVENT 2 ( 2.3%) 2 ( 2.8%) 1 ( 1.0%) 5 ( 2.0%)
  CATARACT OPERATION 1 ( 1.2%) 0  1 ( 1.0%) 2 (<1%)
  ACROCHORDON EXCISION 0  1 ( 1.4%) 0  1 (<1%)
  EYE LASER SURGERY 1 ( 1.2%) 0  0  1 (<1%)
  SKIN LESION EXCISION 0  1 ( 1.4%) 0  1 (<1%)
           
EAR AND LABYRINTH DISORDERS



  ANY EVENT 1 ( 1.2%) 1 ( 1.4%) 2 ( 2.1%) 4 ( 1.6%)
  VERTIGO 0  1 ( 1.4%) 1 ( 1.0%) 2 (<1%)
  CERUMEN IMPACTION 0  0  1 ( 1.0%) 1 (<1%)
  EAR PAIN 1 ( 1.2%) 0  0  1 (<1%)
           
CONGENITAL, FAMILIAL AND GENETIC DISORDERS



  ANY EVENT 0  2 ( 2.8%) 1 ( 1.0%) 3 ( 1.2%)
  VENTRICULAR SEPTAL DEFECT 0  2 ( 2.8%) 1 ( 1.0%) 3 ( 1.2%)
           
NEOPLASMS BENIGN, MALIGNANT AND UNSPECIFIED (INCL CYSTS AND POLYPS)



  ANY EVENT 0  1 ( 1.4%) 2 ( 2.1%) 3 ( 1.2%)
  COLON CANCER 0  0  1 ( 1.0%) 1 (<1%)
  MALIGNANT FIBROUS HISTIOCYTOMA 0  0  1 ( 1.0%) 1 (<1%)
  PROSTATE CANCER 0  1 ( 1.4%) 0  1 (<1%)
           
REPRODUCTIVE SYSTEM AND BREAST DISORDERS



  ANY EVENT 2 ( 2.3%) 1 ( 1.4%) 0  3 ( 1.2%)
  BENIGN PROSTATIC HYPERPLASIA 1 ( 1.2%) 1 ( 1.4%) 0  2 (<1%)
  PELVIC PAIN 1 ( 1.2%) 0  0  1 (<1%)
           
HEPATOBILIARY DISORDERS



  ANY EVENT 1 ( 1.2%) 0  0  1 (<1%)
  HYPERBILIRUBINAEMIA 1 ( 1.2%) 0  0  1 (<1%)
           
IMMUNE SYSTEM DISORDERS



  ANY EVENT 0  0  1 ( 1.0%) 1 (<1%)
  HYPERSENSITIVITY 0  0  1 ( 1.0%) 1 (<1%)
           
SOCIAL CIRCUMSTANCES



  ANY EVENT 0  1 ( 1.4%) 0  1 (<1%)
  ALCOHOL USE 0  1 ( 1.4%) 0  1 (<1%)
           
Demographic Table
Pharmacokinetic
Source Code
---
title: "Adverse Events"
order: 2
---


```{r setup script, include=FALSE, purl=FALSE}
invisible_hook_purl <- function(before, options, ...) {
  knitr::hook_purl(before, options, ...)
  NULL
}
knitr::knit_hooks$set(purl = invisible_hook_purl)
```

## Introduction

This guide will show you how pharmaverse packages, along with some from tidyverse, can be used to create an Adverse Events table, using the `{pharmaverseadam}` `ADSL` and `ADAE` data as an input.
## Multiple Approaches Shown

This example includes:

- **ARD-based approach**: `{cards}` + `{tfrmt}` - Creates structured Analysis Results Datasets following CDISC standards, then formats them into tables
- **Classic approach**: `{rtables}` + `{tern}` - Direct table creation from datasets using mature, stable packages

In the examples below, we illustrate two general approaches for creating an Adverse events table.  The first is the classic method of creating summary tables directly from a data set. The second utilizes Analysis Results Datasets—part of the emerging CDISC Analysis Results Standard.

## {rtables} & {tern}

The packages used with a brief description of their purpose are as follows:

* [`{rtables}`](https://insightsengineering.github.io/rtables/): designed to create and display complex tables with R.
* [`{tern}`](https://insightsengineering.github.io/tern/): contains analysis functions to create tables and graphs used for clinical trial reporting.

### Load Data and Required pharmaverse Package

After installation of packages, the first step is to load our pharmaverse packages and input data. Here, we are going to encode missing entries in a data frame `adsl` and `adae`.

Note that `{tern}` depends on `{rtables}` so the latter is automatically attached.

```{r setup, message=FALSE, warning=FALSE, results='hold'}
library(pharmaverseadam)
library(tern)
library(dplyr)

adsl <- adsl %>%
  df_explicit_na()

adae <- adae %>%
  df_explicit_na()
```

### Start preprocessing

Now we will add some pre-processing to add labels ready for display in the table and how the output will be split.

```{r preproc}
adae <- adae %>%
  var_relabel(
    AEBODSYS = "MedDRA System Organ Class",
    AEDECOD = "MedDRA Preferred Term"
  ) %>%
  filter(SAFFL == "Y")

# Define the split function
split_fun <- drop_split_levels
```

### Adverse Events table

Now we create the Adverse Events table.

```{r table}
lyt <- basic_table(show_colcounts = TRUE) %>%
  split_cols_by(var = "ACTARM") %>%
  add_overall_col(label = "All Patients") %>%
  analyze_num_patients(
    vars = "USUBJID",
    .stats = c("unique", "nonunique"),
    .labels = c(
      unique = "Total number of patients with at least one adverse event",
      nonunique = "Overall total number of events"
    )
  ) %>%
  split_rows_by(
    "AEBODSYS",
    child_labels = "visible",
    nested = FALSE,
    split_fun = split_fun,
    label_pos = "topleft",
    split_label = obj_label(adae$AEBODSYS)
  ) %>%
  summarize_num_patients(
    var = "USUBJID",
    .stats = c("unique", "nonunique"),
    .labels = c(
      unique = "Total number of patients with at least one adverse event",
      nonunique = "Total number of events"
    )
  ) %>%
  count_occurrences(
    vars = "AEDECOD",
    .indent_mods = -1L
  ) %>%
  append_varlabels(adae, "AEDECOD", indent = 1L)

result <- build_table(lyt, df = adae, alt_counts_df = adsl)

result
```

## {tfrmt} & {cards}

In the example below, we will use the [{tfrmt}](https://github.com/GSK-Biostatistics/tfrmt) and [{cards}](https://insightsengineering.github.io/cards/) packages to create a adverse events tables.

- The {cards} package creates Analysis Results Datasets (ARDs, which are a part of the [CDISC Analysis Results Standard](https://www.cdisc.org/standards/foundational/analysis-results-standard)).
- The {tfrmt} utilizes ARDs to create tables.

In the example below, we first build an ARD with the needed summary statistics using {cards}.
Then, we use the ARD to build the adverse events table with {tfrmt}.

### Load libraries

```{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
```

------------------------------------------------------------------------

### Step 1: Import data

Subset to safety population. 

```{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")
```

------------------------------------------------------------------------

### Step 2: Data Preparation

#### Analysis

Create an Analysis Result Display (ARD) using the {cards} package.

```{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
```


### Tidy for table


Further refine the ARD for use in tfrmt

```{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)
```

------------------------------------------------------------------------

### Step 3: Creating the `{tfrmt}` Table

```{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
```
 
Cookie Preferences