# Initiate start-up file
source(file.path(rprojroot::find_root("DESCRIPTION"), "inst/startup.R"))
# Working directory requires write permission
if(file.access(".", 2) != 0){
  warning(
    "The working directory '", normalizePath("."), "' is not writable.\n",
    "Please change it to a location with write permission."
  )
}
# CRAN package, please using install.packages() to install
library(haven) 
library(dplyr)
library(rtables)

# Propitiatory Package, please refer appendix of ADRG to install 
library(pilot1wrappers)

Step 1: Read in data

adsl  <- read_xpt(file.path(path$adam, "adsl.xpt")) 
adsl_labels <- var_labels(adsl)

Step 2: Data preparation

adsl <- adsl %>%
  dplyr::filter(
    STUDYID == "CDISCPILOT01",
    ITTFL == "Y"
  ) %>%
  dplyr::mutate(
    TRT01P = factor(TRT01P, levels = c("Placebo", "Xanomeline Low Dose",  "Xanomeline High Dose")),
    AGEGR1 = factor(AGEGR1, levels = c("<65", "65-80", ">80")),
    RACE = factor(RACE, levels = c("WHITE", "BLACK OR AFRICAN AMERICAN", "AMERICAN INDIAN OR ALASKA NATIVE"))
  ) 

Step 3: Summary of Demographic and Baseline Characteristics

# Table layout
vars <- c("AGE", "AGEGR1", "RACE", "HEIGHTBL", "WEIGHTBL", "BMIBL", "MMSETOT")
lyt <- basic_table(title = "Protocol: CDISCPILOT01",
                   subtitles = "Population: Intent-to-Treat",
                   main_footer = paste0("Program: tlf_demographic.Rmd \n" , Sys.time())
) %>%
  split_cols_by("TRT01P") %>%
  add_colcounts() %>%
  analyze(vars, function(x, ...) {
    if (is.numeric(x)) {
      in_rows(
        "Mean (sd)" = c(mean(x), sd(x)),
        "Median" = median(x),
        "Min - Max" = range(x),
        .formats = c("xx.xx (xx.xx)", "xx.xx", "xx.xx - xx.xx")
      )
    } else if (is.factor(x) || is.character(x)) {
      in_rows(.list = list_wrap_x(table)(x))
    } else {
      stop("type not supproted")
    }
  },
  var_labels = adsl_labels[vars])

# Table build
tbl <- build_table(lyt, adsl)

tbl

Step 4: Output

# Output .out file 
tbl %>%
  toString() %>%
  writeLines(con = file.path(path$output, "tlf-demographic.out"))


RConsortium/submissions-pilot1 documentation built on Feb. 25, 2024, 6:14 a.m.