test_that("expanding variable list works", {

test_that("populating a decode variable works", {
prod <- repfun::ru_fillcodedcode(adsl, codedecodevarpairs=c("SEX", "SEXDCD"), varcodelistpairs=c("SEX", "SEXS"), codelistnames=fmtlist) %>% dplyr::arrange(USUBJID)
adsl %>% dplyr::mutate(SEXDCD=ifelse(SEX=='F','Female',ifelse(SEX=='M','Male',NA))) %>% dplyr::arrange(USUBJID) -> qc
attr(prod, 'label') <- 'Subject Level Analysis'

test_that("generating counts and percents works", {
# Add numeric variables to ADSL (and ADAE).
rfenv$G_POPDATA %>% dplyr::mutate(TRT01AN=ifelse(TRT01A=='Placebo',1,ifelse(TRT01A=='Xanomeline Low Dose',2,3)),
RACEN=ifelse(RACE=='AMERICAN INDIAN OR ALASKA NATIVE',1,ifelse(RACE=='BLACK OR AFRICAN AMERICAN',2,ifelse(RACE=='WHITE',3,NA))),
attr(G_POPDATA$AGEGR1N,"label") <- 'Pooled Age Group 1 (n)'
G_POPDATA %>% dplyr::select(STUDYID,USUBJID,SAFFL,TRT01AN,TRT01A) -> G_POPDATA1
adae <- rfenv$adamdata$adae.rda() %>% dplyr::filter(TRTEMFL=='Y') %>% dplyr::inner_join(G_POPDATA1, by=c('STUDYID','USUBJID','SAFFL','TRT01A'))
# AEs: PROD Counts and percents for Body System and Preferred Term.
G_POPDATA2 <- G_POPDATA1 %>% {. ->> POP1} %>% dplyr::mutate(TRT01AN=99,TRT01A='Total') %>% rbind(POP1) %>% dplyr::arrange(TRT01AN,TRT01A)
aes <- adae %>% dplyr::select(STUDYID,USUBJID,TRT01AN,TRT01A,AEBODSYS,AEDECOD) %>% {. ->> ae1} %>% dplyr::mutate(AEDECOD='ANY EVENT') %>% {. ->> ae2} %>%
dplyr::mutate(AEBODSYS='ANY EVENT') %>% rbind(ae2) %>% rbind(ae1) %>% dplyr::arrange(STUDYID,USUBJID,TRT01AN,TRT01A,AEBODSYS,AEDECOD) %>% dplyr::distinct(STUDYID,USUBJID,TRT01AN,TRT01A,AEBODSYS,AEDECOD)
aes <- rbind(aes,aes %>% dplyr::mutate(TRT01AN=99,TRT01A='Total'))
denoms <- G_POPDATA2 %>% dplyr::group_by(TRT01AN,TRT01A) %>% dplyr::summarize(DENOMCNT=n()) %>% dplyr::ungroup()
numers <- aes %>% dplyr::group_by(TRT01AN,TRT01A,AEBODSYS,AEDECOD) %>% dplyr::summarize(NUMERCNT=n()) %>% dplyr::ungroup()
master <- G_POPDATA2 %>% dplyr::distinct(TRT01AN,TRT01A) %>% tidyr::crossing(aes %>% dplyr::select(AEBODSYS,AEDECOD))
numers <- master %>% dplyr::left_join(numers, by=c('TRT01AN','TRT01A','AEBODSYS','AEDECOD')) %>% dplyr::mutate(NUMERCNT=ifelse(is.na(NUMERCNT),0,NUMERCNT))
aeqc   <- numers %>% dplyr::left_join(denoms, by=c('TRT01AN','TRT01A')) %>% dplyr::mutate(PERCENT=100*NUMERCNT/DENOMCNT,
# AEs: PROD Counts and percents for Preferred Terms only.
resultpctdps=2) %>% dplyr::select(TRT01AN,TRT01A,AEDECOD,NUMERCNT,DENOMCNT,PERCENT,tt_result) %>% dplyr::arrange(TRT01AN,TRT01A,AEDECOD)
aes <- adae %>% dplyr::select(STUDYID,USUBJID,TRT01AN,TRT01A,AEDECOD) %>% {. ->> ae1} %>% dplyr::mutate(AEDECOD='ANY EVENT') %>% {. ->> ae2} %>%
rbind(ae1) %>% dplyr::arrange(STUDYID,USUBJID,TRT01AN,TRT01A,AEDECOD) %>% dplyr::distinct(STUDYID,USUBJID,TRT01AN,TRT01A,AEDECOD)
numers <- aes %>% dplyr::group_by(TRT01AN,TRT01A,AEDECOD) %>% dplyr::summarize(NUMERCNT=n()) %>% dplyr::ungroup()
master <- G_POPDATA2 %>% dplyr::distinct(TRT01AN,TRT01A) %>% tidyr::crossing(aes %>% select(AEDECOD))
numers <- master %>% dplyr::left_join(numers, by=c('TRT01AN','TRT01A','AEDECOD')) %>% dplyr::mutate(NUMERCNT=ifelse(is.na(NUMERCNT),0,NUMERCNT))
aeqc2   <- numers %>% dplyr::left_join(denoms, by=c('TRT01AN','TRT01A')) %>% dplyr::mutate(PERCENT=100*NUMERCNT/DENOMCNT,
# Baseline Characteristics: PROD counts and percents.
G_POPDATA %>% dplyr::select(STUDYID,USUBJID,SAFFL,TRT01AN,TRT01A,SEXN,SEX,RACEN,RACE,AGEGR1N,AGEGR1) -> G_POPDATA2
dflst[[v]] <- repfun::ru_freq(G_POPDATA2, dsetindenom=G_POPDATA2, countdistinctvars=c("STUDYID", "USUBJID"),
anyeventvars = NULL, anyeventvalues = NULL, groupminmaxvar=NULL,
totalforvar=c("TRT01AN"), totalid = 99, totaldecode = 'Total',
groupbyvarsdenom=c("STUDYID", "TRT01AN"), resultstyle="NUMERPCT", codedecodevarpairs=c("TRT01AN", "TRT01A", paste0(v,'N'), v),
varcodelistpairs=c(""), codelistnames=list(), resultpctdps=0) %>% {. ->> LBLS} %>%
basechar <- basechar %>% dplyr::arrange(TRT01AN,TRT01A,tt_avid,tt_avnm,tt_svid,tt_svnm)
G_POPDATA2 <- G_POPDATA2 %>% {. ->> POP1} %>% dplyr::mutate(TRT01AN=99,TRT01A='Total') %>% rbind(POP1) %>% dplyr::arrange(TRT01AN,TRT01A)
numers1 <- G_POPDATA2 %>% dplyr::group_by(TRT01AN,TRT01A,AGEGR1N,AGEGR1) %>% dplyr::summarize(NUMERCNT=n()) %>% dplyr::ungroup() %>% dplyr::mutate(tt_avid=1,tt_avnm=paste0(attr(G_POPDATA2$AGEGR1,"label"),', n (%)')) %>% dplyr::rename(tt_svid=AGEGR1N,tt_svnm=AGEGR1)
numers2 <- G_POPDATA2 %>% dplyr::group_by(TRT01AN,TRT01A,SEXN,SEX) %>% dplyr::summarize(NUMERCNT=n()) %>% dplyr::ungroup() %>% dplyr::mutate(tt_avid=2,tt_avnm=paste0(attr(G_POPDATA2$SEX,"label"),', n (%)')) %>% dplyr::rename(tt_svid=SEXN,tt_svnm=SEX)
numers3 <- G_POPDATA2 %>% dplyr::group_by(TRT01AN,TRT01A,RACEN,RACE) %>% dplyr::summarize(NUMERCNT=n()) %>% dplyr::ungroup() %>% dplyr::mutate(tt_avid=3,tt_avnm=paste0(attr(G_POPDATA2$RACE,"label"),', n (%)')) %>% dplyr::rename(tt_svid=RACEN,tt_svnm=RACE)
master  <- G_POPDATA2 %>% distinct(TRT01AN,TRT01A) %>% tidyr::crossing(numers %>% distinct(tt_avid,tt_avnm,tt_svid,tt_svnm))
numers  <- master %>% dplyr::left_join(numers, by=c('TRT01AN','TRT01A','tt_avid','tt_avnm','tt_svid','tt_svnm')) %>% dplyr::mutate(NUMERCNT=ifelse(is.na(NUMERCNT),0,NUMERCNT))
bcqc    <- numers %>% dplyr::left_join(denoms, by=c('TRT01AN','TRT01A')) %>% dplyr::mutate(PERCENT=100*NUMERCNT/DENOMCNT,
# Vital Sign Timepoints: PROD counts and percents.
filter(SAFFL=='Y' & !is.na(ATPTN) & !is.na(AVISITN) & !is.na(TRT01AN)) %>%
ru_labels(varlabels=list('TRT01AN'='Actual Treatment for Period 01 (n)'))
prod_advs <- repfun::ru_freq(advs, dsetindenom=advs, countdistinctvars=c("STUDYID", "USUBJID"),
anyeventvars = NULL, anyeventvalues =  NULL, groupminmaxvar="max(ATPTN)",
totalforvar="TRT01AN(2, 3)", totalid = 9, totaldecode = 'All Xano',
codedecodevarpairs=c("TRT01AN", "TRT01A", "AVISITN", "AVISIT", "PARAMCD", "PARAM", "ATPTN", "ATPT"),
varcodelistpairs=c(""), codelistnames=list(), resultpctdps=0) %>% select(-tt_summarylevel) %>%
advs_tot <- advs %>% mutate(TRT01AN=ifelse(TRT01AN %in% c(2,3),9,TRT01AN),
trt <- advs2 %>% distinct(TRT01AN,TRT01A)
vis <- advs2 %>% distinct(AVISITN,AVISIT)
prm <- advs2 %>% distinct(PARAMCD,PARAM)
mfile <- trt %>% cross_join(vis) %>% cross_join(prm) %>% cross_join(tim) %>% mutate(NUMERCNT=0)
#PROD <- vs_cnts %>% distinct(TRT01AN,TRT01A,AVISITN,AVISIT,PARAMCD,PARAM,ATPTN,ATPT)
#QC <- mfile %>% distinct(TRT01AN,TRT01A,AVISITN,AVISIT,PARAMCD,PARAM,ATPTN,ATPT)
#PROD %>% arrange(PARAMCD,AVISITN,AVISIT) %>% distinct(PARAMCD,AVISITN,AVISIT)
#QC %>% arrange(PARAMCD,AVISITN,AVISIT) %>% distinct(PARAMCD,AVISITN,AVISIT)
tt_result=paste0(NUMERCNT,' (',round(PERCENT,0),'%)')) %>% select(names(prod_advs)) %>%
# Temporary step, remove all attributes.
qc_advs[] <- lapply(qc_advs, function(x) {
prod_advs[] <- lapply(prod_advs, function(x) {