## ----include=FALSE------------------------------------------------------------
knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  out.width = "100%",
  dpi = 150
)

## -----------------------------------------------------------------------------
library(forestly)
library(metalite)
library(metalite.ae)

## -----------------------------------------------------------------------------
adsl <- forestly_adsl
adae <- forestly_adae

adsl$TRTA <- factor(forestly_adsl$TRT01A,
  levels = c("Xanomeline Low Dose", "Placebo"),
  labels = c("Low Dose", "Placebo")
)
adae$TRTA <- factor(forestly_adae$TRTA,
  levels = c("Xanomeline Low Dose", "Placebo"),
  labels = c("Low Dose", "Placebo")
)

## -----------------------------------------------------------------------------
# 1st analysis plan for AE summary
# i.e., overall AE summary including any AEs, drug-related AEs, and serious AEs
plan <- plan(
  analysis = "ae_summary",
  population = "apat",
  observation = "apat",
  parameter = "any;drug-related;serious"
) |>
  # 2nd analysis plan for AE specific
  # i.e., patients with drug-related AEs
  add_plan(
    analysis = "ae_specific",
    population = "apat",
    observation = "apat",
    parameter = "drug-related"
  ) |>
  # 3rd analysis plan for AE listing
  # i.e., listing of patients with serious AEs
  add_plan(
    analysis = "ae_listing",
    population = "apat",
    observation = "apat",
    parameter = "drug-related"
  ) |>
  # 4th analysis plan for AE forest plot
  # i.e., interactive forest plot for drug-related and serious AEs
  add_plan(
    analysis = "ae_forestly",
    population = "apat",
    observation = "apat",
    parameter = "drug-related;serious"
  )

## -----------------------------------------------------------------------------
meta <- meta_adam(population = adsl, observation = adae) |>
  define_plan(plan) |>
  define_analysis(name = "ae_summary", label = "AE Summary Table") |>
  define_analysis(name = "ae_specific", label = "AE Specific Table") |>
  define_analysis(
    name = "ae_listing", label = "AE Listing Table",
    var_name = c("USUBJID", "ASTDY", "AEDECOD", "ADURN", "AESEV", "AESER", "AEREL", "AEOUT"),
    group_by = c("USUBJID", "ASTDY"), page_by = "TRTA"
  ) |>
  define_analysis(name = "ae_forestly", label = "Interactive Forest Plot") |>
  define_population(
    name = "apat", group = "TRTA", id = "USUBJID",
    subset = SAFFL == "Y", label = "All Patient as Treated"
  ) |>
  define_observation(
    name = "apat", group = "TRTA",
    subset = SAFFL == "Y", label = "All Patient as Treated"
  ) |>
  define_parameter(
    name = "any",
    subset = NULL,
    label = "Any AEs",
    var = "AEDECOD", soc = "AEBODSYS",
    term1 = "", term2 = ""
  ) |>
  define_parameter(
    name = "drug-related",
    subset = toupper(AREL) == "RELATED",
    label = "Drug-related AEs",
    var = "AEDECOD", soc = "AEBODSYS",
    term1 = "Drug Related", term2 = ""
  ) |>
  define_parameter(
    name = "serious",
    subset = toupper(AESER) == "Y",
    label = "Serious AEs",
    var = "AEDECOD", soc = "AEBODSYS",
    term1 = "Serious", term2 = ""
  ) |>
  meta_build()

## -----------------------------------------------------------------------------
meta |>
  prepare_ae_summary(
    population = "apat",
    observation = "apat",
    parameter = "any;drug-related"
  ) |>
  format_ae_summary() |>
  gt_ae_summary(
    source = "Source:  [CDISCpilot: adam-adsl; adae]",
    analysis = "ae_summary" # Provide analysis type defined in meta$analysis
  )

## -----------------------------------------------------------------------------
meta |>
  prepare_ae_specific(
    population = "apat",
    observation = "apat",
    parameter = "drug-related"
  ) |>
  format_ae_specific() |>
  gt_ae_specific(
    meddra_version = "24.0",
    source = "Source:  [CDISCpilot: adam-adsl; adae]",
    analysis = "ae_specific" # Provide analysis type defined in meta$analysis
  )

## -----------------------------------------------------------------------------
meta |>
  prepare_ae_listing(
    population = "apat",
    observation = "apat",
    parameter = "drug-related",
    analysis = "ae_listing" # Provide analysis type defined in meta$analysis
  ) |>
  format_ae_listing() |>
  react_ae_listing(
    default_page_size = 15
  )

## -----------------------------------------------------------------------------
meta |>
  prepare_ae_forestly() |>
  format_ae_forestly() |>
  ae_forestly()

