arframe TFL Gallery
  1. Tables
  2. Adverse Events by System Organ Class and Preferred Term
  • Getting Started
    • Installation

  • Tables
    • Study Conduct
    • Enrollment by Country and Site

    • Study Population
    • Demographics Summary
    • Medical History
    • Prior Medication
    • Disposition Summary
    • Analysis Populations

    • Extent of Exposure
    • Concomitant Medications
    • Extent of Exposure

    • Safety
    • Adverse Events by System Organ Class and Preferred Term
    • AEs Related to Study Drug
    • Common Adverse Events
    • Adverse Events by Grade / Intensity
    • Overall Safety Summary
    • Adverse Events with Event Counts
    • Exposure-Adjusted Adverse Events
    • Adverse Events by Subgroup
    • Serious Adverse Events by SOC and PT
    • AEs Leading to Study Drug Discontinuation
    • Death Summary
    • Vital Signs
    • Laboratory Results - Chemistry
    • Laboratory Shift Table
    • Laboratory Worst Toxicity Grade
    • Laboratory Marked Abnormalities
    • Electrocardiogram Summary

    • Efficacy
    • Time to Event Summary
    • Best Overall Response

  • Listings
    • Adverse Event Listing
    • Demographic Characteristics Listing
    • Medical History Listing
    • Vital Signs Listing
    • Laboratory Test Results Listing
    • Concomitant Medications Listing

  • Figures
    • Kaplan-Meier Plot
    • Swimmer Plot
    • Waterfall Plot

On this page

  • Setup
  • Data Preparation
  • arframe Pipeline
  • Rendered Table
  1. Tables
  2. Adverse Events by System Organ Class and Preferred Term

Adverse Events by System Organ Class and Preferred Term

Summary of Treatment-Emergent Adverse Events by SOC and PT

Setup

See Prerequisites for installation instructions.

library(arframe)
library(pharmaverseadam)
library(dplyr, warn.conflicts = FALSE)
library(tidyr)
library(cards)

adsl_saf <- pharmaverseadam::adsl |>
  blank_to_na() |>
  filter(SAFFL == "Y", TRT01A != "Screen Failure")

adae_teae <- pharmaverseadam::adae |>
  blank_to_na() |>
  filter(TRTEMFL == "Y", TRT01A != "Screen Failure")

arm_levels <- c("Placebo", "Xanomeline Low Dose", "Xanomeline High Dose")

arm_n <- adsl_saf |>
  filter(TRT01A %in% arm_levels) |>
  count(TRT01A) |>
  pull(n, name = TRT01A)
arm_n <- arm_n[arm_levels]

Data Preparation

  • dplyr
  • cards
# ── Helper: n (%) string ──
n_pct <- function(n, denom) sprintf("%d (%.1f)", n, n / denom * 100)

# ── Denominators ──
N_total <- nrow(adsl_saf)
N_arm   <- arm_n

# ── Any TEAE (overall row) ──
any_teae_arm <- adae_teae |>
  distinct(USUBJID, TRT01A) |>
  count(TRT01A) |>
  filter(TRT01A %in% arm_levels) |>
  mutate(value = mapply(n_pct, n, N_arm[TRT01A])) |>
  select(TRT01A, value) |>
  pivot_wider(names_from = TRT01A, values_from = value)

any_teae_total <- adae_teae |>
  distinct(USUBJID) |>
  nrow() |>
  (\(n) tibble(Total = n_pct(n, N_total)))()

any_teae_row <- bind_cols(
  tibble(soc = "TOTAL SUBJECTS WITH AN EVENT", pt = "TOTAL SUBJECTS WITH AN EVENT", row_type = "overall"),
  any_teae_arm,
  any_teae_total
)

# ── SOC-level summary (one subject counted once per SOC) ──
soc_arm <- adae_teae |>
  distinct(USUBJID, TRT01A, AEBODSYS) |>
  count(TRT01A, AEBODSYS) |>
  filter(TRT01A %in% arm_levels) |>
  mutate(value = mapply(n_pct, n, N_arm[TRT01A])) |>
  select(TRT01A, AEBODSYS, value) |>
  pivot_wider(names_from = TRT01A, values_from = value)

soc_total <- adae_teae |>
  distinct(USUBJID, AEBODSYS) |>
  count(AEBODSYS) |>
  mutate(Total = n_pct(n, N_total)) |>
  select(AEBODSYS, Total)

soc_arm <- soc_arm |>
  mutate(across(all_of(arm_levels), ~ replace_na(.x, "0 (0.0)")))

soc_wide <- left_join(soc_arm, soc_total, by = "AEBODSYS") |>
  mutate(
    soc      = AEBODSYS,
    pt       = AEBODSYS,
    row_type = "soc",
    .before  = 1
  ) |>
  select(-AEBODSYS)

# ── PT-level summary (one subject counted once per SOC/PT) ──
pt_arm <- adae_teae |>
  distinct(USUBJID, TRT01A, AEBODSYS, AEDECOD) |>
  count(TRT01A, AEBODSYS, AEDECOD) |>
  filter(TRT01A %in% arm_levels) |>
  mutate(value = mapply(n_pct, n, N_arm[TRT01A])) |>
  select(TRT01A, AEBODSYS, AEDECOD, value) |>
  pivot_wider(names_from = TRT01A, values_from = value)

pt_total <- adae_teae |>
  distinct(USUBJID, AEBODSYS, AEDECOD) |>
  count(AEBODSYS, AEDECOD) |>
  mutate(Total = n_pct(n, N_total)) |>
  select(AEBODSYS, AEDECOD, Total)

pt_arm <- pt_arm |>
  mutate(across(all_of(arm_levels), ~ replace_na(.x, "0 (0.0)")))

pt_wide <- left_join(pt_arm, pt_total, by = c("AEBODSYS", "AEDECOD")) |>
  mutate(
    soc      = AEBODSYS,
    pt       = AEDECOD,
    row_type = "pt",
    .before  = 1
  ) |>
  select(-AEBODSYS, -AEDECOD)

# ── Compute sort order: SOC frequency descending, PT frequency descending ──
soc_order <- soc_wide |>
  left_join(
    adae_teae |>
      distinct(USUBJID, AEBODSYS) |>
      count(AEBODSYS, name = "soc_n"),
    by = c("soc" = "AEBODSYS")
  ) |>
  arrange(desc(soc_n)) |>
  pull(soc)

pt_order <- pt_wide |>
  left_join(
    adae_teae |>
      distinct(USUBJID, AEBODSYS, AEDECOD) |>
      count(AEBODSYS, AEDECOD, name = "pt_n"),
    by = c("soc" = "AEBODSYS", "pt" = "AEDECOD")
  ) |>
  arrange(match(soc, soc_order), desc(pt_n)) |>
  select(-pt_n)

# ── Interleave SOC header row + PT rows, sorted ──
ae_wide <- bind_rows(
  any_teae_row,
  bind_rows(
    lapply(soc_order, function(s) {
      soc_row <- filter(soc_wide, soc == s)
      pt_rows  <- filter(pt_order, soc == s)
      bind_rows(soc_row, pt_rows)
    })
  )
) |>
  mutate(across(
    where(is.character) & !c(soc, pt, row_type),
    ~ replace_na(.x, "0 (0.0)")
  ))

n_vec <- c(arm_n, Total = N_total)
ae_ard <- ard_stack_hierarchical(
  data        = adae_teae,
  variables   = c(AEBODSYS, AEDECOD),
  by          = TRT01A,
  denominator = adsl_saf,
  id          = USUBJID,
  overall     = TRUE,
  over_variables = TRUE
) |>
  sort_ard_hierarchical(sort = "descending")

ae_wide_cards <- fr_wide_ard(
  ae_ard,
  statistic = "{n} ({p}%)",
  decimals  = c(p = 1),
  label     = c(
    "..ard_hierarchical_overall.." = "TOTAL SUBJECTS WITH AN EVENT",
    AEBODSYS = "System Organ Class",
    AEDECOD  = "Preferred Term"
  )
)

arframe Pipeline

The rendered table below uses the dplyr data prep (ae_wide). The cards tab produces an equivalent ae_wide_cards — swap it in to use the cards path instead.

ae_wide |>
  fr_table() |>
  fr_titles(
    "Table 14.3.1",
    "Summary of Treatment-Emergent Adverse Events by System Organ Class and Preferred Term",
    "Safety Population"
  ) |>
  fr_cols(
    soc      = fr_col(visible = FALSE),
    pt       = fr_col("System Organ Class\n  Preferred Term", width = 3.5),
    row_type = fr_col(visible = FALSE),
    !!!setNames(
      lapply(arm_levels, function(a) fr_col(a, align = "decimal")),
      arm_levels
    ),
    Total = fr_col("Total", align = "decimal"),
    .n = n_vec
  ) |>
  fr_header(bold = TRUE, align = "center") |>
  fr_rows(
    group_by  = "soc",
    indent_by = "pt"
  ) |>
  fr_styles(
    fr_row_style(rows = fr_rows_matches("row_type", value = "soc"), bold = TRUE),
    fr_row_style(rows = fr_rows_matches("row_type", value = "overall"), bold = TRUE)
  ) |>
  fr_footnotes(
    "TEAE = Treatment-Emergent Adverse Event. Subjects are counted once per SOC and once per PT.",
    "Percentages based on N per treatment arm (Safety Population).",
    "SOC rows sorted by descending subject frequency; PT rows sorted within SOC by descending frequency."
  )

Rendered Table

Table 14.3.1
Summary of Treatment-Emergent Adverse Events by System Organ Class and Preferred Term
Safety Population
System Organ Class
Preferred Term
Placebo
(N=86)
Xanomeline High Dose
(N=72)
Xanomeline Low Dose
(N=96)
Total
(N=254)
TOTAL SUBJECTS WITH AN EVENT65 (75.6)68 (94.4)84 (87.5)217 (85.4)
GENERAL DISORDERS AND ADMINISTRATION SITE CONDITIONS21 (24.4)36 (50.0)51 (53.1)108 (42.5)
APPLICATION SITE PRURITUS 6 ( 7.0)21 (29.2)23 (24.0) 50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1 ( 1.4) 0 1 ( 0.4)
COLON CANCER 0 0 1 ( 1.0) 1 ( 0.4)
MALIGNANT FIBROUS HISTIOCYTOMA 0 0 1 ( 1.0) 1 ( 0.4)
HEPATOBILIARY DISORDERS 1 ( 1.2) 0 0 1 ( 0.4)
HYPERBILIRUBINAEMIA 1 ( 1.2) 0 0 1 ( 0.4)
SOCIAL CIRCUMSTANCES 0 1 ( 1.4) 0 1 ( 0.4)
ALCOHOL USE 0 1 ( 1.4) 0 1 ( 0.4)
IMMUNE SYSTEM DISORDERS 0 0 1 ( 1.0) 1 ( 0.4)
HYPERSENSITIVITY 0 0 1 ( 1.0) 1 ( 0.4)
TEAE = Treatment-Emergent Adverse Event. Subjects are counted once per SOC and once per PT.
Percentages based on N per treatment arm (Safety Population).
SOC rows sorted by descending subject frequency; PT rows sorted within SOC by descending frequency.
/opt/quarto/share/rmd/rmd.R 01APR2026 09:52:26
Source Code
---
title: "Adverse Events by System Organ Class and Preferred Term"
subtitle: "Summary of Treatment-Emergent Adverse Events by SOC and PT"
execute:
  echo: true
  eval: true
---


```{r}
#| label: prereqs
#| include: false
library(arframe)
fr_theme(hlines = "header", font_family = "Courier New")
blank_to_na <- function(df) {
  df[] <- lapply(df, function(x) {
    if (is.character(x)) x[x == ""] <- NA_character_
    x
  })
  df
}
```

## Setup

See [Prerequisites](../install.qmd) for installation instructions.

```{r}
#| label: setup
library(arframe)
library(pharmaverseadam)
library(dplyr, warn.conflicts = FALSE)
library(tidyr)
library(cards)

adsl_saf <- pharmaverseadam::adsl |>
  blank_to_na() |>
  filter(SAFFL == "Y", TRT01A != "Screen Failure")

adae_teae <- pharmaverseadam::adae |>
  blank_to_na() |>
  filter(TRTEMFL == "Y", TRT01A != "Screen Failure")

arm_levels <- c("Placebo", "Xanomeline Low Dose", "Xanomeline High Dose")

arm_n <- adsl_saf |>
  filter(TRT01A %in% arm_levels) |>
  count(TRT01A) |>
  pull(n, name = TRT01A)
arm_n <- arm_n[arm_levels]
```


## Data Preparation

::: {.panel-tabset}

### dplyr

```{r}
#| label: dplyr-code

# ── Helper: n (%) string ──
n_pct <- function(n, denom) sprintf("%d (%.1f)", n, n / denom * 100)

# ── Denominators ──
N_total <- nrow(adsl_saf)
N_arm   <- arm_n

# ── Any TEAE (overall row) ──
any_teae_arm <- adae_teae |>
  distinct(USUBJID, TRT01A) |>
  count(TRT01A) |>
  filter(TRT01A %in% arm_levels) |>
  mutate(value = mapply(n_pct, n, N_arm[TRT01A])) |>
  select(TRT01A, value) |>
  pivot_wider(names_from = TRT01A, values_from = value)

any_teae_total <- adae_teae |>
  distinct(USUBJID) |>
  nrow() |>
  (\(n) tibble(Total = n_pct(n, N_total)))()

any_teae_row <- bind_cols(
  tibble(soc = "TOTAL SUBJECTS WITH AN EVENT", pt = "TOTAL SUBJECTS WITH AN EVENT", row_type = "overall"),
  any_teae_arm,
  any_teae_total
)

# ── SOC-level summary (one subject counted once per SOC) ──
soc_arm <- adae_teae |>
  distinct(USUBJID, TRT01A, AEBODSYS) |>
  count(TRT01A, AEBODSYS) |>
  filter(TRT01A %in% arm_levels) |>
  mutate(value = mapply(n_pct, n, N_arm[TRT01A])) |>
  select(TRT01A, AEBODSYS, value) |>
  pivot_wider(names_from = TRT01A, values_from = value)

soc_total <- adae_teae |>
  distinct(USUBJID, AEBODSYS) |>
  count(AEBODSYS) |>
  mutate(Total = n_pct(n, N_total)) |>
  select(AEBODSYS, Total)

soc_arm <- soc_arm |>
  mutate(across(all_of(arm_levels), ~ replace_na(.x, "0 (0.0)")))

soc_wide <- left_join(soc_arm, soc_total, by = "AEBODSYS") |>
  mutate(
    soc      = AEBODSYS,
    pt       = AEBODSYS,
    row_type = "soc",
    .before  = 1
  ) |>
  select(-AEBODSYS)

# ── PT-level summary (one subject counted once per SOC/PT) ──
pt_arm <- adae_teae |>
  distinct(USUBJID, TRT01A, AEBODSYS, AEDECOD) |>
  count(TRT01A, AEBODSYS, AEDECOD) |>
  filter(TRT01A %in% arm_levels) |>
  mutate(value = mapply(n_pct, n, N_arm[TRT01A])) |>
  select(TRT01A, AEBODSYS, AEDECOD, value) |>
  pivot_wider(names_from = TRT01A, values_from = value)

pt_total <- adae_teae |>
  distinct(USUBJID, AEBODSYS, AEDECOD) |>
  count(AEBODSYS, AEDECOD) |>
  mutate(Total = n_pct(n, N_total)) |>
  select(AEBODSYS, AEDECOD, Total)

pt_arm <- pt_arm |>
  mutate(across(all_of(arm_levels), ~ replace_na(.x, "0 (0.0)")))

pt_wide <- left_join(pt_arm, pt_total, by = c("AEBODSYS", "AEDECOD")) |>
  mutate(
    soc      = AEBODSYS,
    pt       = AEDECOD,
    row_type = "pt",
    .before  = 1
  ) |>
  select(-AEBODSYS, -AEDECOD)

# ── Compute sort order: SOC frequency descending, PT frequency descending ──
soc_order <- soc_wide |>
  left_join(
    adae_teae |>
      distinct(USUBJID, AEBODSYS) |>
      count(AEBODSYS, name = "soc_n"),
    by = c("soc" = "AEBODSYS")
  ) |>
  arrange(desc(soc_n)) |>
  pull(soc)

pt_order <- pt_wide |>
  left_join(
    adae_teae |>
      distinct(USUBJID, AEBODSYS, AEDECOD) |>
      count(AEBODSYS, AEDECOD, name = "pt_n"),
    by = c("soc" = "AEBODSYS", "pt" = "AEDECOD")
  ) |>
  arrange(match(soc, soc_order), desc(pt_n)) |>
  select(-pt_n)

# ── Interleave SOC header row + PT rows, sorted ──
ae_wide <- bind_rows(
  any_teae_row,
  bind_rows(
    lapply(soc_order, function(s) {
      soc_row <- filter(soc_wide, soc == s)
      pt_rows  <- filter(pt_order, soc == s)
      bind_rows(soc_row, pt_rows)
    })
  )
) |>
  mutate(across(
    where(is.character) & !c(soc, pt, row_type),
    ~ replace_na(.x, "0 (0.0)")
  ))

n_vec <- c(arm_n, Total = N_total)
```

### cards

```{r}
#| label: cards-code
ae_ard <- ard_stack_hierarchical(
  data        = adae_teae,
  variables   = c(AEBODSYS, AEDECOD),
  by          = TRT01A,
  denominator = adsl_saf,
  id          = USUBJID,
  overall     = TRUE,
  over_variables = TRUE
) |>
  sort_ard_hierarchical(sort = "descending")

ae_wide_cards <- fr_wide_ard(
  ae_ard,
  statistic = "{n} ({p}%)",
  decimals  = c(p = 1),
  label     = c(
    "..ard_hierarchical_overall.." = "TOTAL SUBJECTS WITH AN EVENT",
    AEBODSYS = "System Organ Class",
    AEDECOD  = "Preferred Term"
  )
)
```

:::


## arframe Pipeline

The rendered table below uses the **dplyr** data prep (`ae_wide`). The cards tab produces an equivalent `ae_wide_cards` — swap it in to use the cards path instead.

```{r}
#| label: pipeline
#| eval: false
ae_wide |>
  fr_table() |>
  fr_titles(
    "Table 14.3.1",
    "Summary of Treatment-Emergent Adverse Events by System Organ Class and Preferred Term",
    "Safety Population"
  ) |>
  fr_cols(
    soc      = fr_col(visible = FALSE),
    pt       = fr_col("System Organ Class\n  Preferred Term", width = 3.5),
    row_type = fr_col(visible = FALSE),
    !!!setNames(
      lapply(arm_levels, function(a) fr_col(a, align = "decimal")),
      arm_levels
    ),
    Total = fr_col("Total", align = "decimal"),
    .n = n_vec
  ) |>
  fr_header(bold = TRUE, align = "center") |>
  fr_rows(
    group_by  = "soc",
    indent_by = "pt"
  ) |>
  fr_styles(
    fr_row_style(rows = fr_rows_matches("row_type", value = "soc"), bold = TRUE),
    fr_row_style(rows = fr_rows_matches("row_type", value = "overall"), bold = TRUE)
  ) |>
  fr_footnotes(
    "TEAE = Treatment-Emergent Adverse Event. Subjects are counted once per SOC and once per PT.",
    "Percentages based on N per treatment arm (Safety Population).",
    "SOC rows sorted by descending subject frequency; PT rows sorted within SOC by descending frequency."
  )
```


## Rendered Table

```{r}
#| label: rendered-table
#| echo: false
#| ref.label: pipeline
```

Open-source TFL reference collection

 

CDISC Pilot Study (CDISCPILOT01) • pharmaverseadam datasets