': Summary of Treatment Emergent Adverse Events'

test_that("appending data sets works", {
# Append mtcars and airquality using package function.
# Use dplyr to append the same data frames and put the row names into the same format as above.

rs_setup(D_POPDATA=repfun::adsl %>% dplyr::filter(SAFFL =='Y'), D_SUBJID=c("STUDYID","USUBJID"), R_ADAMDATA="../../data")
repfun::ru_labels(varlabels=list('TRT01AN'='Actual Treatment for Period 01 (n)')) -> G_POPDATA
adae <- tibble::as_tibble(rfenv$adamdata$adae.rda()) %>% dplyr::inner_join(G_POPDATA, by=c('STUDYID','USUBJID','SAFFL','TRT01A'))
prod <- repfun::ru_stackvar(dsetin=aesum_p,varsin=c('AEBODSYS','AEDECOD'),varout='SYSPREF',varlabel='Body System/Preferred Term')
qc <- aesum_p %>% dplyr::mutate(SYSPREF=paste0(AEBODSYS,'/','\\line',AEDECOD)) %>% repfun::ru_labels(varlabels=list('SYSPREF'='Body System/Preferred Term')) %>%

test_that("generating summary statistics works", {
G_POPDATA %>% dplyr::select(STUDYID,TRT01AN,TRT01A,AGE) %>% {. ->> pop1} %>% base::rbind(pop1 %>% dplyr::mutate(TRT01AN=99, TRT01A='Total')) %>% dplyr::filter(!is.na(AGE)) %>%
dplyr::ungroup() %>% select(STUDYID, TRT01AN, TRT01A, tt_result, tt_svnm) %>%
G_POPDATA %>% dplyr::select(STUDYID,TRT01AN,TRT01A,TRTDURD) %>% {. ->> pop1} %>%
base::rbind(pop1 %>% dplyr::mutate(TRT01AN=99, TRT01A='Total')) %>%
tidyr::pivot_longer(!c(STUDYID,TRT01AN,TRT01A), names_to="tt_svnm", values_to="tt_result") %>% dplyr::ungroup() %>%
dplyr::select(STUDYID, TRT01AN, TRT01A, tt_result, tt_svnm) %>%
attr(qc_basechars$tt_svnm,"label") <- 'Statistical Parameter Name'
attr(qc_basechars$tt_avnm,"label") <- 'Analysis Variable Name'
# Vital signs part 1: Precision constant across PARAMCD/PARAM.
dplyr::filter(!is.na(AVISITN) & (DTYPE=='AVERAGE')) -> advs2
attr(advs2$TRT01AN,"label") <- 'Actual Treatment for Period 01 (n)'
codedecodevarpairs=c("TRT01AN", "TRT01A", "PARAMCD", "PARAM", "AVISITN", "AVISIT"),
advs2 %>% dplyr::select(STUDYID,TRT01AN,TRT01A,PARAMCD,PARAM,AVISITN,AVISIT,AVAL) %>% {. ->> pop1} %>% base::rbind(pop1 %>% dplyr::mutate(TRT01AN=99, TRT01A='Total')) %>%
tidyr::pivot_longer(!c(STUDYID,TRT01AN,TRT01A,PARAMCD,PARAM,AVISITN,AVISIT), names_to="tt_svnm", values_to="tt_result") %>% dplyr::ungroup() %>%
dplyr::select(STUDYID, TRT01AN, TRT01A, PARAMCD, PARAM, AVISITN, AVISIT, tt_result, tt_svnm) -> qc_vtlsigns
attr(qc_vtlsigns$tt_svnm,"label") <- 'Statistical Parameter Name'
# Vital signs part 2: Precision differs by PARAMCD/PARAM.
decodes <- advs2 %>% distinct(PARAMCD, PARAM)
advs2 %>% dplyr::select(STUDYID,USUBJID,TRT01AN,TRT01A,PARAMCD,AVISITN,AVISIT,AVAL) %>% dplyr::arrange(USUBJID,TRT01AN,TRT01A,AVISITN,AVISIT) %>%
dplyr::group_by(USUBJID,TRT01AN,TRT01A,AVISITN,AVISIT) %>% tidyr::pivot_wider(names_from=PARAMCD, values_from=AVAL) -> advs2_t
analysisvars=c("BMI", "BSA", "DIABP", "MAP", "PULSE", "SYSBP", "TEMP", "WEIGHT"),
analysisvardps=list("BMI"=1, "BSA"=2, "DIABP"=3, "MAP"=4, "PULSE"=1, "SYSBP"=2, "TEMP"=3, "WEIGHT"=4),
dplyr::arrange(tt_avid, TRT01AN, AVISITN, tt_svid) %>%
Max=max(AVAL, na.rm=TRUE)) %>% ungroup() %>%
dplyr::select(STUDYID, TRT01AN, TRT01A, PARAMCD, PARAM, AVISITN, AVISIT, tt_result, tt_svnm) %>% dplyr::arrange(PARAMCD,TRT01AN,AVISITN) %>% dplyr::select(names(vtlsigns_t)) -> qc_vtlsigns_t
attr(qc_vtlsigns_t$tt_svnm,"label") <- 'Statistical Parameter Name'