SAFFL=ifelse((row_number() %% 10) == 0,'N',SAFFL)) %>%
repfun::ru_labels(varlabels=list('TRT01AN'='Actual Treatment for Period 01 (n)','SAFFL'='Safety Population Flag')) -> G_POPDATA
repfun::ru_getdata(G_POPDATA, c("STUDYID", "USUBJID"), keeppopvars=c("TRT01AN", "TRT01A", "SAFFL")) %>%
pop <- G_POPDATA %>% select(STUDYID,USUBJID,TRT01AN,TRT01A,SAFFL) %>% dplyr::filter(SAFFL=='Y')
adaeqc <- rfenv$adamdata$adae.rda() %>% dplyr::select(-c('SAFFL','TRT01A')) %>% dplyr::inner_join(pop,by=c('STUDYID','USUBJID')) %>%

add_decode <- repfun::ru_groupbyvars(c("TRTCD", "TRTGRP", "ATOXGRN", "AEDECOD", "AEBODSYS"), c("TRTCD", "TRTGRP", "ATOXGRN", "ATOXGR"), TRUE)
# Remove decode variables from the list.
rem_decode <- repfun::ru_groupbyvars(c("TRTCD", "TRTGRP", "ATOXGRN", "AEDECOD", "AEBODSYS"), c("TRTCD", "TRTGRP", "ATOXGRN", "ATOXGR"), FALSE)
testthat::expect_equal(add_decode, c("TRTCD", "TRTGRP", "ATOXGRN", "ATOXGR", "AEDECOD", "AEBODSYS"))
testthat::expect_equal(rem_decode, c("TRTCD", "ATOXGRN", "AEDECOD", "AEBODSYS"))

test_that("ru_libname works for rds and rda files", {
# Read ADSL RDA file, restrict to SAFETY subjects.
adsl <- adamdata1$adsl.rda() %>% dplyr::filter(SAFFL=='Y') -> adsl1
# Save it as an RDS file to a temporary folder.
# Read that RDS file in using ru_libnames.

"ru_libname works for rds and rda files"

D_POPDATA=repfun::adsl %>% dplyr::filter(SAFFL =='Y') %>% dplyr::mutate(TRT01AN=ifelse(TRT01A=='Placebo',1,ifelse(TRT01A=='Xanomeline Low Dose',2,3))) %>%
repfun::ru_labels(varlabels=list('TRT01AN'='Actual Treatment for Period 01 (n)')),
D_TITLE1=paste0('Table ',tlfid,': Summary of Treatment Emergent Adverse Events'),
# Process ADAE - derive counts and percents.
aesum <- repfun::ru_freq(rfenv$adamdata$adae.rda() %>% dplyr::select(-SAFFL) %>% repfun::ru_getdata(rfenv$G_POPDATA, c("STUDYID", "USUBJID"), keeppopvars=c("TRT01AN", "TRT01A")),
resultpctdps=0) %>% dplyr::arrange(TRT01AN,TRT01A,AEBODSYS,tt_summarylevel,AEDECOD,NUMERCNT,DENOMCNT) %>% repfun::ru_align("tt_result")
# Prod Table 1:  Summary of Adverse Events - DDDATA only.
dddatasetlabel=paste0('DD Dataframe for AE Table ',rfenv$G_DSPLYNUM),
prod1 <- prod %>% mutate_at(vars(AEBODSYS,AEDECOD,tt_01,tt_02,tt_03,tt_99),as.character) %>% ru_labels(.,varlabels=lapply(prod,attr,"label"))
qc1 <- aesum %>% dplyr::mutate(TRT01A=paste0(TRT01A,'\\line (N=',DENOMCNT,')\\line n (%)')) %>%
repfun::ru_denorm(varstodenorm=c("tt_result", "PERCENT"), groupbyvars=c("tt_summarylevel", "AEBODSYS", "AEDECOD"), acrossvar="TRT01AN",
acrossvarlabel="TRT01A", acrossvarprefix=c("tt_", "tt_p")) %>% {lapply(., attr, "label") ->> LBLS; .} %>% dplyr::arrange(AEBODSYS,tt_summarylevel,AEDECOD) %>%
dplyr::select(-starts_with('tt_p'),-tt_summarylevel) %>% dplyr::group_by(AEBODSYS) %>% dplyr::group_modify(~ add_row(.x,.after=Inf)) %>%
dplyr::ungroup() %>% dplyr::mutate(dplyr::across(dplyr::everything(), ~ tidyr::replace_na(.x, ""))) %>% dplyr::mutate(dplyr::across(dplyr::everything(), as.character)) %>%
qc1 %>% group_by(AEBODSYS) %>% mutate(lastrow=ifelse(row_number()==n(),TRUE,FALSE),
frstpage=ifelse(lastrow & !PAGEVAR==lag(PAGEVAR),TRUE,FALSE)) %>% filter(!(lastrow & frstpage)) %>% select(-c(lastrow,frstpage)) -> qc1
attr(qc1,'label') <- 'DD Dataframe for AE Table 1'
# Generate 2nd DD data set and compare with QC version.
SOCterms <- aesum %>% dplyr::distinct(AEBODSYS,AEDECOD)
repfun::ru_list(aesum %>% dplyr::filter(!(AEBODSYS %in% names(SOCcnts[SOCcnts>=20]))),
prod2 <- prod %>% mutate_at(vars(AEBODSYS,AEDECOD,tt_01,tt_02,tt_03,tt_99),as.character) %>% ru_labels(.,varlabels=lapply(prod,attr,"label"))
qc2 <- aesum %>% dplyr::filter(!(AEBODSYS %in% names(SOCcnts[SOCcnts>=20]))) %>% dplyr::mutate(TRT01A=paste0(TRT01A,'\\line (N=',DENOMCNT,')\\line n (%)')) %>%
dplyr::ungroup() %>% dplyr::mutate(dplyr::across(dplyr::everything(), ~ tidyr::replace_na(.x, ""))) %>% dplyr::mutate(dplyr::across(dplyr::everything(), as.character)) %>% repfun::ru_labels(varlabels=LBLS,style='base') %>%
attr(qc2,'label') <- 'DD Dataframe for AE Table 2'