---
title: "Example 8: Patient Profile"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Example 8: Patient Profile}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

options(rmarkdown.html_vignette.check_title = FALSE)

```
Below is an example showing a patient profile report. Every organization has
their own way of presenting patient profiles.  This is an example of a 
"master and detail" type of report.

## Program

Note the following about this example:

  * A loop is used to append separate tables for each patient. 
  * The profile for each patient is shown on a separate page.
  * The program makes extensive use of user-defined formats from the **fmtr** package.
  
```{r eval=FALSE, echo=TRUE}
library(sassy)

options("logr.notes" = FALSE,
        "logr.autolog" = TRUE)

# Get temp location for log and report output
tmp <- tempdir()

lf <- log_open(file.path(tmp, "example8.log"))


# Get data ----------------------------------------------------------------
sep("Get data")


# Get sample data path
pth <- system.file("extdata", package = "sassy")

put("Open data library")
libname(sdtm, pth, "csv")


# Set labels --------------------------------------------------------------
sep("Set labels")

put("DM labels")
labels(sdtm$DM) <- list(ARM = "Treatment Group",
                        SITEID = "Centre",
                        SUBJID = "Subject", 
                        SEX = "Sex", 
                        AGE = "Age (yrs)",
                        RACE = "Race", 
                        BRTHDTC = "Birth Date",
                        ARMCD = "Treatment Code"
) |> put()
put("AE labels")
labels(sdtm$AE) <- list(AESTDTC = "Event Start Date",
                        AEENDTC = "Event Stop Date",
                        AESTDY = "Start",
                        AEENDY = "End",
                        AESOC = "System Organ Class",
                        AESEV = "Severityᵃ",
                        AESER = "Serious",
                        AEREL = "Related") |> put()


# Apply formats ----------------------------------------------------------
sep("Apply formats")

sevfmt <- value(condition(x == "MODERATE", "Moderate"),
                condition(x == "SEVERE", "Severe"),
                condition(x == "MILD", "Mild"))


relfmt <- value(condition(x == "RELATED", "Yes"),
                condition(x == "NOT RELATED", "No"),
                condition(x == "PROBABLY RELATED", "Probably"),
                condition(x == "POSSIBLY RELATED", "Possibly")) 

serfmt <- value(condition(x == "N", "No"),
                condition(x == "Y", "Yes")) 


sexfmt <- value(condition(x == "M", "Male"),
                condition(x == "F", "Female"),
                condition(TRUE, "Unknown")) 

racefmt <- value(condition(x == "WHITE", "White"),
                 condition(x == "BLACK OR AFRICAN AMERICAN", "Black"),
                 condition(x == "UNKNOWN", "Unknown"),
                 condition(x == "ASIAN", "Asian")) 

armfmt <- value(condition(x == "ARM A", "Placebo"),
                condition(x == "ARM B", "Dose 50mg"),
                condition(x == "ARM C", "Dose 100mg"),
                condition(x == "ARM D", "Competitor")) 


formats(sdtm$DM) <- list(SEX = sexfmt, 
                         RACE = racefmt,
                         ARM = armfmt)

formats(sdtm$AE) <- list(AESEV = sevfmt,
                         AEREL = relfmt, 
                         AESER = serfmt) 


# Prepare data ------------------------------------------------------------

sep("Prepare data")

put("Select desired columns from DM dataset")
datastep(sdtm$DM, 
         keep = v(USUBJID, ARM, SITEID, SUBJID, SEX, AGE, RACE, BRTHDTC), {}) -> dm 


# Split dm data by subject id
dmlst <- split(dm, factor(dm$USUBJID))

put("Select desired columns from AE dataset")
datastep(sdtm$AE, 
         keep = v(USUBJID, AESTDTC, AEENDTC, AESTDY, 
                  AEENDY, AESOC, AESEV, AESER, AEREL), {}) -> ae

# Split ae data by subject id
aelst <- split(ae, factor(ae$USUBJID))


# Create report -----------------------------------------------------------
sep("Create report")

# Create report first, outside loop
rpt <- create_report(file.path(tmp, "output/example8.docx"), 
                     font = "Arial", output_type = "DOCX")

# Loop on subjects
for (id in names(dmlst)) {
  
  dm_sub <- dmlst[[id]]
  ae_sub <- aelst[[id]]
  
  tb1 <- create_table(dm_sub, width = 8, borders = "outside") |> 
    titles("Listing 1.1 Subjects Narratives of Adverse Events",
           paste0("Subject: ", dm_sub[[1, 1]]), bold = TRUE) |> 
    define(AGE, align = "left")
  
  if (!is.null(ae_sub)) {
    tb2 <- create_table(ae_sub, borders = "outside", width = 8) |> 
      spanning_header("AESTDY", "AEENDY", label ="Study Day") |> 
      define(USUBJID, visible = FALSE) |> 
      define(AESOC, width = 2) |> 
      define(AESTDY, align = "left") |> 
      define(AEENDY, align = "left") |> 
      footnotes("ᵃSeverity: 01=Mild, 02=Moderate, 03=Severe, 04=Life Threatening, 05=Fatal",
                paste0("ᵇAction Taken: 01=None, 02=Investigational product dose altered, ",
                       "03=Medication taken, 04=Hospitalized, 05=Removed from study, ",
                       "06=Investigational product discontinued, 07=Transfusion performed,",
                       "88=Other"))
  }
  
  # Append table content
  rpt <- rpt |>  add_content(tb1, page_break = FALSE) |> 
    add_content(tb2)
  
}

put("Write out report")
res <- write_report(rpt)


# Clean Up ----------------------------------------------------------------


sep("Clean up")

log_close()


# View log
# file.show(lf)

# View report
# file.show(res$modified_path)

```

## Output

Here is page 11 out of 87 pages in the output report:

<img src="../man/images/profile.png" alt="Patient Profile example listing"/>


## Log

Here is most of the log from the above program:
```
=========================================================================
Log Path: C:/Users/dbosa/AppData/Local/Temp/Rtmp6lCP5X/log/example8.log
Program Path: C:/packages/Testing/procs/ProcsPatientProfile.R
Working Directory: C:/packages/Testing/procs
User Name: dbosa
R Version: 4.3.1 (2023-06-16 ucrt)
Machine: SOCRATES x86-64
Operating System: Windows 10 x64 build 22621
Base Packages: stats graphics grDevices utils datasets methods base Other
Packages: tidylog_1.0.2 ggplot2_3.4.2 patchwork_1.1.3 procs_1.0.3
reporter_1.4.1 libr_1.2.8 fmtr_1.5.9 logr_1.3.4 common_1.0.8 sassy_1.2.0
Log Start Time: 2023-09-06 21:24:18.59308
=========================================================================

=========================================================================
Get data
=========================================================================

# library 'sdtm': 7 items
- attributes: csv not loaded
- path: C:/Users/dbosa/AppData/Local/R/win-library/4.3/sassy/extdata
- items:
  Name Extension Rows Cols     Size        LastModified
1   AE       csv  150   27  88.5 Kb 2023-09-06 21:14:37
2   DM       csv   87   24  45.5 Kb 2023-09-06 21:14:37
3   DS       csv  174    9  34.1 Kb 2023-09-06 21:14:37
4   EX       csv   84   11  26.4 Kb 2023-09-06 21:14:37
5   IE       csv    2   14  13.4 Kb 2023-09-06 21:14:37
6   SV       csv  685   10  70.3 Kb 2023-09-06 21:14:37
7   VS       csv 3358   17 467.4 Kb 2023-09-06 21:14:37

=========================================================================
Set labels
=========================================================================

DM labels

$ARM
[1] "Treatment Group"

$SITEID
[1] "Centre"

$SUBJID
[1] "Subject"

$SEX
[1] "Sex"

$AGE
[1] "Age (yrs)"

$RACE
[1] "Race"

$BRTHDTC
[1] "Birth Date"

$ARMCD
[1] "Treatment Code"


AE labels

$AESTDTC
[1] "Event Start Date"

$AEENDTC
[1] "Event Stop Date"

$AESTDY
[1] "Start"

$AEENDY
[1] "End"

$AESOC
[1] "System Organ Class"

$AESEV
[1] "Severityᵃ"

$AESER
[1] "Serious"

$AEREL
[1] "Related"


=========================================================================
Apply formats
=========================================================================

# A user-defined format: 3 conditions
  Name Type      Expression    Label Order
1  obj    U x == "MODERATE" Moderate    NA
2  obj    U   x == "SEVERE"   Severe    NA
3  obj    U     x == "MILD"     Mild    NA

# A user-defined format: 4 conditions
  Name Type              Expression    Label Order
1  obj    U          x == "RELATED"      Yes    NA
2  obj    U      x == "NOT RELATED"       No    NA
3  obj    U x == "PROBABLY RELATED" Probably    NA
4  obj    U x == "POSSIBLY RELATED" Possibly    NA

# A user-defined format: 2 conditions
  Name Type Expression Label Order
1  obj    U   x == "N"    No    NA
2  obj    U   x == "Y"   Yes    NA

# A user-defined format: 3 conditions
  Name Type Expression   Label Order
1  obj    U   x == "M"    Male    NA
2  obj    U   x == "F"  Female    NA
3  obj    U       TRUE Unknown    NA

# A user-defined format: 4 conditions
  Name Type                       Expression   Label Order
1  obj    U                     x == "WHITE"   White    NA
2  obj    U x == "BLACK OR AFRICAN AMERICAN"   Black    NA
3  obj    U                   x == "UNKNOWN" Unknown    NA
4  obj    U                     x == "ASIAN"   Asian    NA

# A user-defined format: 4 conditions
  Name Type   Expression      Label Order
1  obj    U x == "ARM A"    Placebo    NA
2  obj    U x == "ARM B"  Dose 50mg    NA
3  obj    U x == "ARM C" Dose 100mg    NA
4  obj    U x == "ARM D" Competitor    NA

=========================================================================
Prepare data
=========================================================================

Select desired columns from DM dataset

datastep: columns decreased from 24 to 8

# A tibble: 87 × 8
   USUBJID    ARM   SITEID SUBJID SEX     AGE RACE                      BRTHDTC   
   <chr>      <chr> <chr>  <chr>  <chr> <dbl> <chr>                     <date>    
 1 ABC-01-049 ARM D 01     049    M        39 WHITE                     1966-11-12
 2 ABC-01-050 ARM B 01     050    M        47 WHITE                     1958-12-19
 3 ABC-01-051 ARM A 01     051    M        34 WHITE                     1972-05-02
 4 ABC-01-052 ARM C 01     052    F        45 WHITE                     1961-06-27
 5 ABC-01-053 ARM B 01     053    F        26 WHITE                     1980-04-07
 6 ABC-01-054 ARM D 01     054    M        44 WHITE                     1962-09-13
 7 ABC-01-055 ARM C 01     055    F        47 BLACK OR AFRICAN AMERICAN 1959-06-11
 8 ABC-01-056 ARM A 01     056    M        31 WHITE                     1975-05-02
 9 ABC-01-113 ARM D 01     113    M        74 WHITE                     1932-02-08
10 ABC-01-114 ARM B 01     114    F        72 WHITE                     1934-07-09
# ℹ 77 more rows
# ℹ Use `print(n = ...)` to see more rows

Select desired columns from AE dataset

datastep: columns decreased from 27 to 9

# A tibble: 150 × 9
   USUBJID    AESTDTC    AEENDTC    AESTDY AEENDY AESOC                AESEV AESER AEREL
   <chr>      <chr>      <chr>       <dbl>  <dbl> <chr>                <chr> <chr> <chr>
 1 ABC-01-049 2006-12-28 2007-03-13     52    127 Investigations       MODE… N     NOT …
 2 ABC-01-049 2006-12-28 2007-03-13     52    127 Investigations       MODE… N     NOT …
 3 ABC-01-049 2007       2007           NA     NA Nervous system diso… MILD  N     NOT …
 4 ABC-01-049 2007-01-11 2007-03-13     66    127 Investigations       MODE… N     NOT …
 5 ABC-01-049 2007       2007           NA     NA Musculoskeletal and… MILD  N     NOT …
 6 ABC-01-050 2006-11-08 2006-11-15      7     14 Skin and subcutaneo… MILD  N     NOT …
 7 ABC-01-050 2006-12-17 2006-12-17     46     46 Respiratory, thorac… MILD  N     NOT …
 8 ABC-01-051 2007-01-01 2007-01-01     61     61 Nervous system diso… MILD  N     NOT …
 9 ABC-01-051 2007-03-06 2007-03-09    125    128 Nervous system diso… MILD  N     NOT …
10 ABC-01-051 2007-11-12 2007-11-14    376    378 Nervous system diso… MILD  N     NOT …
# ℹ 140 more rows
# ℹ Use `print(n = ...)` to see more rows

=========================================================================
Create report
=========================================================================

Write out report

# A report specification: 87 pages
- file_path: 'C:\Users\dbosa\AppData\Local\Temp\Rtmp6lCP5X/output/example8.docx'
- output_type: DOCX
- units: inches
- orientation: landscape
- margins: top 0.5 bottom 0.5 left 1 right 1
- line size/count: 9/42
- content: 
# A table specification:
- data: tibble 'dm_sub' 1 rows 8 cols
- show_cols: all
- use_attributes: all
- width: 8
- title 1: 'Listing 1.1 Subjects Narratives of Adverse Events'
- title 2: 'Subject: ABC-01-049'
- define: AGE align='left' 
# A table specification:
- data: tibble 'ae_sub' 5 rows 9 cols
- show_cols: all
- use_attributes: all
- width: 8
- footnote 1: 'ᵃSeverity: 01=Mild, 02=Moderate, 03=Severe, 04=Life Threatening, 05=Fatal'
- footnote 2: 'ᵇAction Taken: 01=None, 02=Investigational product dose altered, 03=Medication taken, 04=Hospitalized, 05=Removed from study, 06=Investigational product discontinued, 07=Transfusion performed,88=Other'
- spanning_header: from='AESTDY' to='AEENDY' 'Study Day' level=1 
- define: USUBJID visible='FALSE' 
- define: AESOC width=2 
- define: AESTDY align='left' 
- define: AEENDY align='left' 
...

=========================================================================
Clean up
=========================================================================

=========================================================================
Log End Time: 2023-09-06 21:24:25.764916
Log Elapsed Time: 0 00:00:07
=========================================================================

```


Next: [Example 9: Forest Plot](sassy-forest.html)
