## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(tplyr2)
library(knitr)
tplyr2_options(IBMRounding = FALSE)

## ----step1--------------------------------------------------------------------
ae_settings <- layer_settings(
  distinct_by = "USUBJID",
  format_strings = list(
    n_counts = f_str("xxx (xx.x%)", "distinct_n", "distinct_pct")
  )
)

spec <- tplyr_spec(
  cols = "TRTA",
  where = TRTEMFL == "Y",
  pop_data = pop_data(cols = c("TRTA" = "TRT01A")),
  layers = tplyr_layers(
    group_count(c("AEBODSYS", "AEDECOD"), settings = ae_settings)
  )
)

result <- tplyr_build(spec, tplyr_adae, pop_data = tplyr_adsl)
kable(head(result[, c("rowlabel1", "rowlabel2", "res1", "res2", "res3")], 12))

## ----step1-headern------------------------------------------------------------
kable(tplyr_header_n(result))

## ----step2--------------------------------------------------------------------
ae_settings <- layer_settings(
  distinct_by = "USUBJID",
  total_row = TRUE,
  total_row_label = "Any adverse event",
  missing_subjects = TRUE,
  missing_subjects_label = "No adverse events",
  format_strings = list(
    n_counts = f_str("xxx (xx.x%)", "distinct_n", "distinct_pct")
  )
)

spec <- tplyr_spec(
  cols = "TRTA",
  where = TRTEMFL == "Y",
  pop_data = pop_data(cols = c("TRTA" = "TRT01A")),
  layers = tplyr_layers(
    group_count(c("AEBODSYS", "AEDECOD"), settings = ae_settings)
  )
)

result <- tplyr_build(spec, tplyr_adae, pop_data = tplyr_adsl)

# The "Any adverse event" and "No adverse events" summary rows
summary_rows <- result[result$rowlabel1 %in% c("Any adverse event", "No adverse events"), ]
kable(summary_rows[, c("rowlabel1", "res1", "res2", "res3")])

## ----step3--------------------------------------------------------------------
ae_settings <- layer_settings(
  distinct_by = "USUBJID",
  total_row = TRUE,
  total_row_label = "Any adverse event",
  format_strings = list(
    n_counts = f_str("xxx (xx.x%)", "distinct_n", "distinct_pct")
  ),
  assoc_test = assoc_test(
    fn = function(m) suppressWarnings(fisher.test(m)$p.value),
    reference   = "Placebo",
    comparisons = c("Xanomeline High Dose", "Xanomeline Low Dose"),
    format      = f_str("x.xxx", "p")
  )
)

spec <- tplyr_spec(
  cols = "TRTA",
  where = TRTEMFL == "Y",
  pop_data = pop_data(cols = c("TRTA" = "TRT01A")),
  layers = tplyr_layers(
    group_count(c("AEBODSYS", "AEDECOD"), settings = ae_settings)
  )
)

result <- tplyr_build(spec, tplyr_adae, pop_data = tplyr_adsl)
kable(head(result[, c("rowlabel1", "rowlabel2", "res1", "res2", "res3",
                      "pval1", "pval2")], 12))

## ----step4--------------------------------------------------------------------
spec <- tplyr_spec(
  cols = "TRTA",
  where = TRTEMFL == "Y",
  pop_data = pop_data(cols = c("TRTA" = "TRT01A")),
  layers = tplyr_layers(
    group_count("AEDECOD",
      settings = layer_settings(
        distinct_by = "USUBJID",
        order_count_method = "bycount",
        result_order_var = "distinct_n",
        format_strings = list(
          n_counts = f_str("xxx (xx.x%)", "distinct_n", "distinct_pct")
        ),
        risk_diff = list(
          comparisons = list(c("Xanomeline High Dose", "Placebo")),
          format = f_str("xx.x (xx.x, xx.x)", "rdiff", "lower", "upper")
        )
      )
    )
  )
)

result <- tplyr_build(spec, tplyr_adae, pop_data = tplyr_adsl)
result <- result[order(result$ord_layer_1), ]

kable(head(result[, c("rowlabel1", "res1", "res2", "res3", "rdiff1")], 10))

## ----step5--------------------------------------------------------------------
# Rebuild the Step 3 table (nested + Fisher p-values)
result <- tplyr_build(tplyr_spec(
  cols = "TRTA",
  where = TRTEMFL == "Y",
  pop_data = pop_data(cols = c("TRTA" = "TRT01A")),
  layers = tplyr_layers(
    group_count(c("AEBODSYS", "AEDECOD"), settings = ae_settings)
  )
), tplyr_adae, pop_data = tplyr_adsl)

display <- result |>
  as_display(labels = TRUE) |>
  collapse_row_labels("rowlabel1", "rowlabel2", indent = "   ")

kable(head(display, 14))

