-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsassy-profile.Rmd
More file actions
432 lines (323 loc) · 13.6 KB
/
sassy-profile.Rmd
File metadata and controls
432 lines (323 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
---
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)