-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsassy-figureby.Rmd
More file actions
306 lines (234 loc) · 8.92 KB
/
sassy-figureby.Rmd
File metadata and controls
306 lines (234 loc) · 8.92 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
---
title: "Example 6: Figure with By Group"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Example 6: Figure with By Group}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
The **sassy** system gives you capabilities that few other R packages can match.
The system not only support reports with by-groups. You can even apply
a by-group to a figure.
## Program
Note the following about this example:
* The plot is created as a single plot with no by-groups
* The plot is added to the report with the `add_content()` function, just
like the figures in the previous example.
* The `page_by()` function on the `create_plot()` statement generates the
paging for both the report and plot.
```{r eval=FALSE, echo=TRUE}
library(sassy)
library(ggplot2)
options("logr.autolog" = TRUE,
"logr.notes" = FALSE)
# Get path to temp directory
tmp <- tempdir()
# Get path to sample data
pkg <- system.file("extdata", package = "sassy")
# Open log
lgpth <- log_open(file.path(tmp, "example6.log"))
# Prepare Data ------------------------------------------------------------
sep("Prepare Data")
put("Load data files")
libname(dat, pkg, "csv", filter = c("DM", "VS"))
put("Prepare factor levels")
visit_levels <- c("SCREENING", "DAY 1", "WEEK 2", "WEEK 4", "WEEK 6",
"WEEK 8", "WEEK 12", "WEEK 16") |> put()
arm_levels <- c("ARM A", "ARM B") |> put()
test_codes <- c("SYSBP", "DIABP", "PULSE", "RESP") |> put()
put("Prepare data for analysis")
datastep(dat$DM, merge = dat$VS, merge_by = v(STUDYID, USUBJID),
keep = v(STUDYID, USUBJID, ARM, VISIT, VSTESTCD, VSORRES),
merge_in = v(inDM, inVS),
where = expression(VISIT != "END OF STUDY EARLY TERMINATION" &
ARM %in% c("ARM A", "ARM B") &
VSTESTCD %in% test_codes
),
{
# Assign factors to VISIT and ARM
VISIT <- factor(VISIT, levels = visit_levels)
ARM <- factor(ARM, levels = arm_levels)
VSTESTCD <- factor(VSTESTCD, levels = test_codes)
if (!inDM & inVS) {
delete()
}
}) -> vitals
# Create Plot -------------------------------------------------------------
sep("Create Plot")
put("Assign Colors")
arm_cols <- c("ARM B" = "#1f77b4", "ARM A" = "#2f2f2f")
put("Define Boxplot")
p_box <- ggplot2::ggplot(
vitals,
ggplot2::aes(x = VISIT, y = VSORRES, fill = ARM, colour = ARM)
) +
ggplot2::geom_boxplot(
position = ggplot2::position_dodge(width = 0.75),
width = 0.6,
outlier.size = 0.7,
alpha = 0.9
) +
ggplot2::scale_fill_manual(values = arm_cols) +
ggplot2::scale_colour_manual(values = arm_cols) +
ggplot2::labs(
x = NULL,
y = "Lab Value",
fill = NULL,
colour = NULL
) +
ggplot2::theme_bw(base_size = 10) +
ggplot2::theme(
legend.position = "bottom",
panel.grid.major.x = ggplot2::element_blank(),
plot.title = ggplot2::element_text(face = "bold", hjust = 0),
plot.caption = ggplot2::element_text(hjust = 0)
)
put("Create format for lab codes")
lbfmt <- value(condition(x == "SYSBP", "Systolic Blood Pressure (mmHg)"),
condition(x == "DIABP", "Diastolic Blood Pressure (mmHg)"),
condition(x == "PULSE", "Pulse (bpm)"),
condition(x == "RESP", "Respirations (bpm)"))
# Report ------------------------------------------------------------------
sep("Report")
put("Create plot object definition")
plt <- create_plot(p_box, height = 4, width = 7, borders = "outside") |>
titles("Figure 10. Box Plot: Median and Interquartile Range of Vital Signs by Treatment Arm",
bold = TRUE, font_size = 12, align = "left") |>
page_by(VSTESTCD, label = "Lab Test: ", format = lbfmt, blank_row = "none") |>
footnotes(
"Source: example6.rtf. {version$version.string}",
"Note: Boxes span the interquartile range (25th to 75th percentile); horizontal line = median;",
"whiskers = 1.5×IQR; individual outliers are those beyond this range.",
font_size = 9, italics = TRUE, blank_row = "none"
)
put("Create report output path")
pth <- file.path(tempdir(), "example6.rtf")
put("Create report")
rpt <- create_report(pth, font = "Arial", font_size = 10, output_type = "RTF") |>
page_header("Sponsor: Company", right = "Study: ABC", blank_row = "below") |>
add_content(plt) |>
page_footer("Date Produced: {fapply(Sys.Date(), 'date7')}", right = "Page [pg] of [tpg]")
put("Write report to file system")
write_report(rpt)
put("Close log")
log_close()
# View report
# file.show(pth)
# View log
# file.show(lgpth)
```
## Output
And here are the first two pages of the report:
<img src="../man/images/vs_pg1.png" align="center" alt="Figure by example image 1" />
<img src="../man/images/vs_pg2.png" align="center" alt="Figure by example image 2" />
## Log
Here is the log for the above program:
```
=========================================================================
Log Path: C:/Users/dbosa/AppData/Local/Temp/Rtmpq0yZA5/log/example6.log
Program Path: C:/Studies/Testing1/P6b.R
Working Directory: C:/Studies/Testing1
User Name: dbosa
R Version: 4.4.3 (2025-02-28 ucrt)
Machine: SOCRATES x86-64
Operating System: Windows 10 x64 build 26100
Base Packages: stats graphics grDevices utils datasets methods base
Other Packages: tidylog_1.1.0 ggplot2_3.5.1 procs_1.0.7 reporter_1.4.5
libr_1.3.9 logr_1.3.9 fmtr_1.7.0 common_1.1.4 sassy_1.2.9
Log Start Time: 2025-12-12 14:27:32.328372
=========================================================================
=========================================================================
Prepare Data
=========================================================================
Load data files
# library 'dat': 2 items
- attributes: csv not loaded
- path: C:/Users/dbosa/AppData/Local/R/win-library/4.4/sassy/extdata
- items:
Name Extension Rows Cols Size LastModified
1 DM csv 87 24 45.8 Kb 2025-12-12 08:37:33
2 VS csv 3358 17 467.7 Kb 2025-12-12 08:37:33
Prepare factor levels
SCREENING
DAY 1
WEEK 2
WEEK 4
WEEK 6
WEEK 8
WEEK 12
WEEK 16
ARM A
ARM B
SYSBP
DIABP
PULSE
RESP
Prepare data for analysis
datastep: columns decreased from 24 to 6
# A tibble: 1,231 × 6
STUDYID USUBJID ARM VISIT VSTESTCD VSORRES
<chr> <chr> <fct> <fct> <fct> <dbl>
1 ABC ABC-01-050 ARM B SCREENING DIABP 80
2 ABC ABC-01-050 ARM B DAY 1 DIABP 78
3 ABC ABC-01-050 ARM B WEEK 2 DIABP 64
4 ABC ABC-01-050 ARM B WEEK 4 DIABP 86
5 ABC ABC-01-050 ARM B WEEK 6 DIABP 70
6 ABC ABC-01-050 ARM B WEEK 8 DIABP 80
7 ABC ABC-01-050 ARM B WEEK 12 DIABP 64
8 ABC ABC-01-050 ARM B WEEK 16 DIABP 82
9 ABC ABC-01-050 ARM B SCREENING PULSE 76
10 ABC ABC-01-050 ARM B DAY 1 PULSE 68
# ℹ 1,221 more rows
# ℹ Use `print(n = ...)` to see more rows
=========================================================================
Create Plot
=========================================================================
Assign Colors
Define Boxplot
Create format for lab codes
# A user-defined format: 4 conditions
Name Type Expression Label Order
1 obj U x == "SYSBP" Systolic Blood Pressure (mmHg) NA
2 obj U x == "DIABP" Diastolic Blood Pressure (mmHg) NA
3 obj U x == "PULSE" Pulse (bpm) NA
4 obj U x == "RESP" Respirations (bpm) NA
=========================================================================
Report
=========================================================================
Create plot object definition
Create report output path
Create report
Write report to file system
# A report specification: 4 pages
- file_path: 'C:\Users\dbosa\AppData\Local\Temp\Rtmpq0yZA5/example6.rtf'
- output_type: RTF
- units: inches
- orientation: landscape
- margins: top 0.5 bottom 0.5 left 1 right 1
- line size/count: 9/42
- page_header: left=Sponsor: Company right=Study: ABC
- page_footer: left=Date Produced: 12DEC25 center= right=Page [pg] of [tpg]
- content:
# A plot specification:
- data: 1231 rows, 6 cols
- layers: 1
- height: 4
- width: 7
- page by: VSTESTCD
- title 1: 'Figure 10. Box Plot: Median and Interquartile Range of Vital Signs by Treatment Arm'
- footnote 1: 'Source: example6.rtf. R version 4.4.3 (2025-02-28 ucrt)'
- footnote 2: 'Note: Boxes span the interquartile range (25th to 75th percentile); horizontal line = median;'
- footnote 3: 'whiskers = 1.5×IQR; individual outliers are those beyond this range.'
Close log
=========================================================================
Log End Time: 2025-12-12 14:27:40.667776
Log Elapsed Time: 0 00:00:08
=========================================================================
```
Next: [Example 7: Survival Analysis](sassy-survival.html)