## -----------------------------------------------------------------------------
#| echo: false
#| output: false
library(dplyr)
library(amp.dm)


## ----setup--------------------------------------------------------------------
#| echo: false
#| output: false
library(amp.dm)
library(dplyr)


## ----readdata-----------------------------------------------------------------
#| output: false
dm <- read_data(system.file("example/SOURCE/DM.xpt",package="amp.dm"), 
                comment = "demographic data")
ex <- read_data(system.file("example/SOURCE/EX.xpt",package="amp.dm"), 
                comment = "dosing data")
pc <- read_data(system.file("example/SOURCE/PC.xpt",package="amp.dm"), 
                comment = "pk data")
vs <- read_data(system.file("example/SOURCE/VS.xpt",package="amp.dm"), 
                comment = "vital signs data")


## ----demog--------------------------------------------------------------------
#| output: false
cmnt("There are duplicate subjects, these are excluded in DM domain")
dm1 <- filterr(dm, !duplicated(USUBJID), comment="Dropped duplicate subjects") |>
  mutate(SEX   = ifelse(SEX=='M', 0, 1),
         TRT   = as.numeric(as.factor(ARM)),
         CNTRY = as.numeric(as.factor(COUNTRY)))

vs1  <- tidyr::pivot_wider(vs,names_from = VSTESTCD, values_from = VSSTRESN) |>
  select(-STUDYID)
subj <- left_joinr(dm1,vs1,by='USUBJID',comment = "Combine covariates")
subj <- select(subj, STUDYID, USUBJID, TRT, CNTRY, SEX, AGE, WEIGHT, HEIGHT, BMI)

srce(CNTRY,dm.COUNTRY)
srce(BMI,c(vs.WEIGHT,vs.HEIGHT),'d')


## ----pkobs--------------------------------------------------------------------
#| output: false
pk <- pc |> 
  mutate(variable = "PKSample",
         STIME    = PCTPTNUM,
         dattim   = as.POSIXct(PCDTC, format="%Y-%m-%dT%H:%M:%S"),
         FLAGPK   = case_when(is.na(PCSTRESN) ~ 1, PCSTRESN==0 ~ 2, .default = 3)) |>
  rename(Stime = PCTPTNUM, DV = PCSTRESN) %>% select(-c(PCTESTCD, STUDYID, PCDTC))


## ----pkdose-------------------------------------------------------------------
#| output: false
dose <- ex|>
  mutate(variable = "Dose",
         STIME    = 0,
         dattim   = as.POSIXct(EXSTDTC, format = "%Y-%m-%dT%H:%M:%S")) %>%
  rename(AMT = EXDOSE) %>% select(-c(EXSTDTC, STUDYID))


## ----combine------------------------------------------------------------------
#| output: false
cmnt(paste("For combining PK/observations with demographics,",
           "records in **first** are used (`all.x=TRUE`)"))

nm <- bind_rows(dose,pk) |> left_join(subj, by="USUBJID") |>
  rename(ID=USUBJID) |>
  time_calc(datetime = "dattim") |>
  mutate(STUDYID = as.numeric(as.factor(STUDYID)),
         CMT     = ifelse(variable=="Dose", 1, 2),
         EVID    = ifelse(CMT==1, 1, 0),
         MDV     = ifelse(CMT==2 & FLAGPK==3, 0, 1)) %>% 
  select(STUDYID, ID, TRT, CMT, AMT, STIME, TIME, TAFD, TALD, DV, EVID, MDV, 
         CNTRY, SEX, AGE, WEIGHT, HEIGHT, BMI, FLAGPK)




## ----write_data---------------------------------------------------------------
#| output: false

attr <- attr_xls(system.file("example/Attr.Template.xlsx",package = "amp.dm"))
nmf  <- attr_add(nm, attr)

# Write csv and/or xpt file (notice file is named same as script)
output_data(nmf, csv = paste0(tempdir(),"/",get_script(), ".csv"), 
            xpt = paste0(tempdir(),"/",get_script(), ".xpt"),   
            attr = paste0(tempdir(),"/",get_script(), ".rds"),   
            readonly = TRUE)
 
# Save current workspace
# lognfo <- get_log()
# save.image(paste0(get_script(),".RData"))


## ----dataset_define-----------------------------------------------------------
#| results: asis
define_tbl(attr, ret="tbl")

# We could directly output a define.pdf
# define_tbl(attr, outnm= paste0("define.",get_script(), ".tex"),
#            show=FALSE, title="Data define overview")


## ----data_stats---------------------------------------------------------------
#| results: asis
#| warning: false
#| message: false
stats_df(nmf,size="\\footnotesize")

# Example for a counts table
nmf2 <- attr_factor(nmf)
counts_df(nmf2, by=c("STUDYID","TRT"), id="ID", capt="Number of records by study")


## ----log_overview-------------------------------------------------------------
#| results: asis
#| warning: false
#| message: false

all_log   <- get_log()

log_df(get_log(), "read_nfo", ret="tbl", capt="Overview of data read-in")
log_df(get_log(), "filterr_nfo", ret="tbl", capt="Overview of data excluded")
log_df(get_log(), "joinr_nfo", ret="tbl", capt="Overview of joined data")


## ----check_nmdata-------------------------------------------------------------
#| results: asis
#| warning: false
#| message: false

check_nmdata(paste0(tempdir(),"/",get_script(), ".csv"), ret="tbl",type=1)
check_nmdata(paste0(tempdir(),"/",get_script(), ".csv"), ret="tbl",type=2)


## ----plot_nmdata--------------------------------------------------------------
#| fig-height: 12
#| fig-width: 8
#| warning: false
#| message: false

plot_vars(nm,ppp=20)


## ----session_table------------------------------------------------------------
#| results: asis
#| warning: false
#| message: false

session_tbl()

