## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE
)

## -----------------------------------------------------------------------------
library(forestly)
library(metalite)
library(dplyr)

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

meta <- meta_adam(population = adsl, observation = adae) |>
  define_plan(plan = plan(
    analysis = "ae_forestly",
    population = "apat",
    observation = "apat",
    parameter = "any;drug-related"
  )) |>
  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"
  ) |>
  define_parameter(
    name = "drug-related",
    subset = toupper(AREL) == "RELATED",
    label = "Drug-related AEs",
    var = "AEDECOD", soc = "AEBODSYS"
  ) |>
  meta_build()

## -----------------------------------------------------------------------------
meta |>
  prepare_ae_forestly() |>
  format_ae_forestly(
    width_term = 5, # width of the column of AE PT term
    # widths of the column of subject with AE counts
    width_n = 2,
    # widths of the column of AE proportions
    width_prop = 2,
    # widths of plotting the AE proportions and differences
    width_fig = 150,
    # vertical space between legend and the table
    footer_space = 100
  ) |>
  ae_forestly()

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

set.seed(1234)
adsl$TRTA <- sample(
  x = c("Control Arm A",
        "Control Arm B",
        "Active Arm (Mono): Drug X",
        "Active Arm (Comb): Drug X + Drug A + Drug B + Drug C",
        "Active Arm: Cumulative Drug X"),
  size = nrow(adsl),
  replace = TRUE
)
adae <- adae |> 
  dplyr::select(-TRTA) |>
  dplyr::left_join(
    adsl,
    by = c("USUBJID"),
    suffix = c("", ".y")
  ) |>
  dplyr::select(-ends_with(".y"))

adsl$TRTA <- factor(
  adsl$TRTA,
  levels = c("Active Arm: Cumulative Drug X",
             "Active Arm (Mono): Drug X",
             "Active Arm (Comb): Drug X + Drug A + Drug B + Drug C",
             "Control Arm A",
             "Control Arm B")
)
adae$TRTA <- factor(
  adae$TRTA,
  levels = c("Active Arm: Cumulative Drug X",
             "Active Arm (Mono): Drug X",
             "Active Arm (Comb): Drug X + Drug A + Drug B + Drug C",
             "Control Arm A",
             "Control Arm B")
)

meta <- meta_adam(population = adsl, observation = adae) |>
  define_plan(plan = plan(
    analysis = "ae_forestly",
    population = "apat",
    observation = "apat",
    parameter = "any;drug-related"
  )) |>
  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"
  ) |>
  define_parameter(
    name = "drug-related",
    subset = toupper(AREL) == "RELATED",
    label = "Drug-related AEs",
    var = "AEDECOD", soc = "AEBODSYS"
  ) |>
  meta_build()

## -----------------------------------------------------------------------------
meta |>
  prepare_ae_forestly() |>
  format_ae_forestly(
    footer_space = 210,
    width_fig = 420,
    display = c("n", "prop", "fig_prop", "fig_diff", "diff"),
    color = c("#66203A", "#00857C", "#6ECEB2", "#BFED33", "#DFEd31")
  ) |>
  ae_forestly()

