# Build an Interactive Baseline Characteristic Table

    library(r2rtf)

    ## Warning: package 'r2rtf' was built under R version 4.4.3

    library(metalite)
    library(metalite.ae)
    library(metalite.sl)

There are 2 key metadata types:

- metadata for the baseline characteristic table
- metadata for the AE subgroup specific table

# Build metadata

## Metadata for baseline characteristic table

The code below builds the subject-level metadata directly from the example dataset.

    adsl <- r2rtf::r2rtf_adsl
    adsl$TRTA <- adsl$TRT01A
    adsl$TRTA <- factor(adsl$TRTA,
      levels = c("Placebo", "Xanomeline Low Dose", "Xanomeline High Dose"),
      labels = c("Placebo", "Low Dose", "High Dose")
    )

    meta_sl <- meta_adam(
      population = adsl,
      observation = adsl
    ) |>
      define_plan(plan = plan(
        analysis = "base_char", population = "apat",
        observation = "apat", parameter = "age;gender;race"
      )) |>
      define_population(
        name = "apat",
        group = "TRTA",
        subset = quote(SAFFL == "Y"),
        var = c("USUBJID", "TRTA", "SAFFL", "AGEGR1", "SEX", "RACE")
      ) |>
      define_observation(
        name = "apat",
        group = "TRTA",
        subset = quote(SAFFL == "Y"),
        var = c("USUBJID", "TRTA", "SAFFL", "AGEGR1", "SEX", "RACE")
      ) |>
      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",
        label = "baseline characteristic table"
      ) |>
      meta_build()

## A metadata of the AE subgroup specific analysis

The AE metadata is built directly from the example subject-level and adverse event datasets.

    adsl_ae <- metalite_sl_adsl
    adsl_ae$RACE <- tools::toTitleCase(adsl_ae$RACE)
    adae <- metalite_sl_adae

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

    meta_ae <- meta_adam(observation = adae, population = adsl_ae) |>
      define_plan(analysis_plan) |>
      define_population(
        name = "apat",
        var = c(
          "USUBJID", "SAFFL", "TRTA", "TRTDUR",
          "SITEID", "SEX", "RACE", "AGE"
        ),
        group = "TRTA",
        subset = SAFFL == "Y",
        label = "All Participants as Treated"
      ) |>
      define_observation(
        name = "wk12",
        var = c(
          "USUBJID", "SAFFL", "TRTA", "SEX", "AEDECOD", "AEBODSYS", "AEREL",
          "AESER", "AEOUT", "AEACN", "AESDTH", "ASTDT", "AENDT"
        ),
        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_parameter(
        name = "ser",
        term1 = "Serious",
        term2 = "",
        subset = AESER == "Y",
        var = "AEDECOD",
        soc = "AEBODSYS",
        label = "Serious AEs"
      ) |>
      define_analysis(
        name = "ae_specific",
        title = "Participants With Drug-Related Adverse Events"
      ) |>
      meta_build()

## Customization (Optional)

If you want to capitalize only the first letter of “RACE” (e.g., Black or african american) or any other character variable, you can customize the `react_base_char` function at the beginning of the code.

    # function to capitalize the first letter of a string that has multiple words
    capitalize_words <- function(x) {
      sapply(x, function(word) {
        paste0(toupper(substr(word, 1, 1)), tolower(substr(word, 2, nchar(word))))
      })
    }

    # 1) In "data_population": extract the RACE values as a character vector
    race_values_pop <- meta_sl[["data_population"]]$RACE # Use $ to get a vector

    # Capitalize the race values
    meta_sl[["data_population"]]$RACE <- capitalize_words(race_values_pop) # Assign back as a vector

    # 2) In "data_observation": extract the RACE values as a character vector
    race_values_obs <- meta_sl[["data_observation"]]$RACE # Use $ to get a vector

    # Capitalize the race values
    meta_sl[["data_observation"]]$RACE <- capitalize_words(race_values_obs) # Assign back as a vector

# Build a reactable

## Baseline characteristic table + Participants With Drug-Related AE

    react_base_char(
      metadata_sl = meta_sl,
      metadata_ae = meta_ae,
      ae_subgroup = c("age", "race", "gender"),
      ae_specific = "rel",
      width = 1200
    )

## Baseline characteristic table + Participants With Serious AE

    react_base_char(
      metadata_sl = meta_sl,
      metadata_ae = meta_ae,
      ae_subgroup = c("age", "race", "gender"),
      ae_specific = "ser",
      col_title = "Participants With Serious Adverse Events",
      width = 1200
    )