# 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
## Protocol: CDISCPILOT01
## Population: Intent-to-Treat
## 
## —————————————————————————————————————————————————————————————————————————————————————————————————
##                                          Placebo       Xanomeline Low Dose   Xanomeline High Dose
##                                          (N=86)              (N=84)                 (N=84)       
## —————————————————————————————————————————————————————————————————————————————————————————————————
## Age                                                                                              
##   Mean (sd)                           75.21 (8.59)        75.67 (8.29)           74.38 (7.89)    
##   Median                                  76.00               77.50                 76.00        
##   Min - Max                           52.00 - 89.00       51.00 - 88.00         56.00 - 88.00    
## Pooled Age Group 1                                                                               
##   <65                                      14                   8                     11         
##   65-80                                    42                  47                     55         
##   >80                                      30                  29                     18         
## Race                                                                                             
##   WHITE                                    78                  78                     74         
##   BLACK OR AFRICAN AMERICAN                 8                   6                     9          
##   AMERICAN INDIAN OR ALASKA NATIVE          0                   0                     1          
## Baseline Height (cm)                                                                             
##   Mean (sd)                          162.57 (11.52)      163.43 (10.42)         165.82 (10.13)   
##   Median                                 162.60              162.60                 165.10       
##   Min - Max                          137.20 - 185.40     135.90 - 195.60       146.10 - 190.50   
## Baseline Weight (kg)                                                                             
##   Mean (sd)                           62.76 (12.77)       67.28 (14.12)         70.00 (14.65)    
##   Median                                  60.55               64.90                 69.20        
##   Min - Max                           34.00 - 86.20      45.40 - 106.10         41.70 - 108.00   
## Baseline BMI (kg/m^2)                                                                            
##   Mean (sd)                           23.64 (3.67)        25.06 (4.27)           25.35 (4.16)    
##   Median                                  23.40               24.30                 24.80        
##   Min - Max                           15.10 - 33.30       17.70 - 40.10         13.70 - 34.50    
## MMSE Total                                                                                       
##   Mean (sd)                           18.05 (4.27)        17.87 (4.22)           18.51 (4.16)    
##   Median                                  19.50               18.00                 20.00        
##   Min - Max                           10.00 - 23.00       10.00 - 24.00         10.00 - 24.00    
## —————————————————————————————————————————————————————————————————————————————————————————————————
## 
## Program: tlf_demographic.Rmd 
## 2024-02-21 17:13:35.590572

Step 4: Output

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