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

## -----------------------------------------------------------------------------
library(metalite)
library(metalite.sl)

## -----------------------------------------------------------------------------
sl_plan <- plan(
  analysis = "base_char",
  population = "apat",
  observation = "apat",
  parameter = "age;gender;race"
)

metadata_sl <- meta_adam(
  population = metalite_sl_adsl,
  observation = metalite_sl_adsl
) |>
  define_plan(sl_plan) |>
  define_population(
    name = "apat",
    group = "TRTA",
    subset = SAFFL == "Y"
  ) |>
  define_parameter(
    name = "age",
    var = "AGE",
    label = "Age (years)",
    vargroup = "AGEGR1"
  ) |>
  define_parameter(name = "gender", var = "SEX", label = "Gender") |>
  define_parameter(name = "race", var = "RACE", label = "Race") |>
  define_analysis(
    name = "base_char",
    title = "Participant Baseline Characteristics by Treatment Group"
  ) |>
  meta_build()

## ----eval = FALSE-------------------------------------------------------------
# metadata_sl |>
#   prepare_base_char(
#     population = "apat",
#     analysis = "base_char",
#     parameter = "age;gender"
#   ) |>
#   format_base_char() |>
#   rtf_base_char(
#     source = "Source: [CDISCpilot: adam-adsl]",
#     path_outdata = tempfile(fileext = ".Rdata"),
#     path_outtable = tempfile(fileext = ".rtf")
#   )

## ----out.width = "100%", out.height = "400px", echo = FALSE, fig.align = "center"----
knitr::include_graphics("pdf/base0char.pdf")

## -----------------------------------------------------------------------------
analysis_plan <- plan(
  analysis = "ae_specific",
  population = "apat",
  observation = "wk12",
  parameter = "rel"
)

metadata_ae <- meta_adam(
  observation = metalite_sl_adae,
  population = metalite_sl_adsl
) |>
  define_plan(analysis_plan) |>
  define_population(
    name = "apat",
    group = "TRTA",
    subset = SAFFL == "Y",
    label = "All Participants as Treated"
  ) |>
  define_observation(
    name = "wk12",
    group = "TRTA",
    subset = SAFFL == "Y",
    label = "Weeks 0 to 12"
  ) |>
  define_parameter(
    name = "rel",
    term1 = "Drug-Related",
    term2 = "",
    subset = AEREL %in% c("POSSIBLE", "PROBABLE"),
    var = "AEDECOD",
    soc = "AEBODSYS",
    label = "Drug-related AEs"
  ) |>
  define_analysis(
    name = "ae_specific",
    title = "Participants With Drug-Related Adverse Events"
  ) |>
  meta_build()

react_base_char(
  metadata_sl = metadata_sl,
  metadata_ae = metadata_ae,
  population = "apat",
  observation = "wk12",
  display_total = TRUE,
  sl_parameter = "age;race",
  ae_subgroup = c("age", "race"),
  ae_specific = "rel",
  width = 1200
)

