Below is another complete example showing how the reporter package can be used to create figures. The example also shows how multiple pieces of content can be added to a single report.
library(tidyverse)
library(sassy)
# Prepare Log -------------------------------------------------------------
options("logr.autolog" = TRUE,
"logr.notes" = FALSE)
# Get temp location for log and report output
<- tempdir()
tmp
# Open log
<- log_open(file.path(tmp, "example4.log"))
lf
# Load and Prepare Data ---------------------------------------------------
sep("Prepare Data")
# Get path to sample data
<- system.file("extdata", package = "reporter")
pkg
# Define data library
libname(sdtm, pkg, "csv", quiet = TRUE)
# Loads data into workspace
lib_load(sdtm)
# Prepare data
<- sdtm.DM %>%
dm_mod select(USUBJID, SEX, AGE, ARM) %>%
filter(ARM != "SCREEN FAILURE") %>%
datastep({
if (AGE >= 18 & AGE <= 24)
= "18 to 24"
AGECAT else if (AGE >= 25 & AGE <= 44)
= "25 to 44"
AGECAT else if (AGE >= 45 & AGE <= 64)
<- "45 to 64"
AGECAT else if (AGE >= 65)
<- ">= 65"
AGECAT
%>% put()
})
put("Get population counts")
<- count(dm_mod, ARM) %>% put()
arm_pop <- count(dm_mod, SEX) %>% put()
sex_pop <- count(dm_mod, AGECAT) %>% put()
agecat_pop
# Convert agecat to factor so rows will sort correctly
$AGECAT <- factor(agecat_pop$AGECAT, levels = c("18 to 24",
agecat_pop"25 to 44",
"45 to 64",
">= 65"))
# Sort agecat
<- agecat_pop %>% arrange(AGECAT)
agecat_pop
# Create Plots ------------------------------------------------------------
<- ggplot(data = arm_pop, aes(x = ARM, y = n)) +
plt1 geom_col(fill = "#0000A0") +
geom_text(aes(label = n), vjust = 1.5, colour = "white") +
labs(x = "Treatment Group", y = "Number of Subjects (n)")
<- ggplot(data = sex_pop, aes(x = SEX, y = n)) +
plt2 geom_col(fill = "#00A000") +
geom_text(aes(label = n), vjust = 1.5, colour = "white") +
labs(x = "Biological Sex", y = "Number of Subjects (n)")
<- ggplot(data = agecat_pop, aes(x = AGECAT, y = n)) +
plt3 geom_col(fill = "#A00000") +
geom_text(aes(label = n), vjust = 1.5, colour = "white") +
labs(x = "Age Categories", y = "Number of Subjects (n)")
# Report ------------------------------------------------------------------
sep("Create and print report")
<- create_plot(plt1, 4.5, 7) %>%
page1 titles("Figure 1.1", "Distribution of Subjects by Treatment Group")
<- create_plot(plt2, 4.5, 7) %>%
page2 titles("Figure 1.2", "Distribution of Subjects by Biological Sex")
<- create_plot(plt3, 4.5, 7) %>%
page3 titles("Figure 1.2", "Distribution of Subjects by Age Category")
<- create_report(file.path(tmp, "./output/example4.rtf"), output_type = "RTF",
rpt font = "Arial") %>%
set_margins(top = 1, bottom = 1) %>%
page_header("Sponsor: Company", "Study: ABC") %>%
add_content(page1) %>%
add_content(page2) %>%
add_content(page3) %>%
footnotes("Program: DM_Figure.R") %>%
page_footer(paste0("Date Produced: ", fapply(Sys.time(), "%d%b%y %H:%M")),
right = "Page [pg] of [tpg]")
<- write_report(rpt)
res
# Clean Up ----------------------------------------------------------------
sep("Clean Up")
# Unload library from workspace
lib_unload(sdtm)
# Close log
log_close()
# View log
writeLines(readLines(lf, encoding = "UTF-8"))
# View report
# file.show(res$file_path)
Here is the log from the above program:
=========================================================================
Log Path: C:/Users/dbosa/AppData/Local/Temp/RtmpwLpEIV/log/example1.log
Program Path: C:\packages\Testing\reporter_example4.R
Working Directory: C:/packages/Testing
User Name: dbosa
R Version: 4.1.2 (2021-11-01)
Machine: SOCRATES x86-64
Operating System: Windows 10 x64 build 19041
Base Packages: stats graphics grDevices utils datasets methods base
Other Packages: tidylog_1.0.2 reporter_1.2.6 libr_1.2.1 fmtr_1.5.4 logr_1.2.7
sassy_1.0.5 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4
readr_2.0.2 tidyr_1.1.4 tibble_3.1.5 ggplot2_3.3.5 tidyverse_1.3.1
Log Start Time: 2021-11-20 23:06:50
=========================================================================
=========================================================================
Prepare Data
=========================================================================
# library 'sdtm': 8 items
- attributes: csv not loaded
- path: C:/Users/dbosa/Documents/R/win-library/4.1/reporter/extdata
- items:
Name Extension Rows Cols Size LastModified
1 AE csv 150 27 88.3 Kb 2021-10-08 15:02:15
2 DA csv 3587 18 528.1 Kb 2021-10-08 15:02:15
3 DM csv 87 24 45.4 Kb 2021-10-08 15:02:15
4 DS csv 174 9 33.9 Kb 2021-10-08 15:02:15
5 EX csv 84 11 26.2 Kb 2021-10-08 15:02:15
6 IE csv 2 14 13.2 Kb 2021-10-08 15:02:15
7 SV csv 685 10 70.2 Kb 2021-10-08 15:02:15
8 VS csv 3358 17 467.3 Kb 2021-10-08 15:02:15
lib_load: library 'sdtm' loaded
select: dropped 20 variables (STUDYID, DOMAIN, SUBJID, RFSTDTC, RFENDTC, <U+0085>)
filter: removed 2 rows (2%), 85 rows remaining
datastep: columns increased from 4 to 5
# A tibble: 85 x 5
USUBJID SEX AGE ARM AGECAT
<chr> <chr> <dbl> <chr> <chr>
1 ABC-01-049 M 39 ARM D 25 to 44
2 ABC-01-050 M 47 ARM B 45 to 64
3 ABC-01-051 M 34 ARM A 25 to 44
4 ABC-01-052 F 45 ARM C 45 to 64
5 ABC-01-053 F 26 ARM B 25 to 44
6 ABC-01-054 M 44 ARM D 25 to 44
7 ABC-01-055 F 47 ARM C 45 to 64
8 ABC-01-056 M 31 ARM A 25 to 44
9 ABC-01-113 M 74 ARM D >= 65
10 ABC-01-114 F 72 ARM B >= 65
# ... with 75 more rows
Get population counts
count: now 4 rows and 2 columns, ungrouped
# A tibble: 4 x 2
ARM n
<chr> <int>
1 ARM A 20
2 ARM B 21
3 ARM C 21
4 ARM D 23
count: now 2 rows and 2 columns, ungrouped
# A tibble: 2 x 2
SEX n
<chr> <int>
1 F 32
2 M 53
count: now 4 rows and 2 columns, ungrouped
# A tibble: 4 x 2
AGECAT n
<chr> <int>
1 >= 65 13
2 18 to 24 5
3 25 to 44 23
4 45 to 64 44
=========================================================================
Create and print report
=========================================================================
# A report specification: 3 pages
- file_path: 'C:\Users\dbosa\AppData\Local\Temp\RtmpwLpEIV/./output/example1.rtf'
- output_type: RTF
- units: inches
- orientation: landscape
- margins: top 1 bottom 1 left 1 right 1
- line size/count: 9/40
- page_header: left=Sponsor: Company right=Study: ABC
- footnote 1: 'Program: DM_Figure.R'
- page_footer: left=Date Produced: 20Nov21 23:06 center= right=Page [pg] of [tpg]
- content:
# A plot specification:
- data: 4 rows, 2 cols
- layers: 2
- height: 4.5
- width: 7
- title 1: 'Figure 1.1'
- title 2: 'Distribution of Subjects by Treatment Group'
# A plot specification:
- data: 2 rows, 2 cols
- layers: 2
- height: 4.5
- width: 7
- title 1: 'Figure 1.2'
- title 2: 'Distribution of Subjects by Biological Sex'
# A plot specification:
- data: 4 rows, 2 cols
- layers: 2
- height: 4.5
- width: 7
- title 1: 'Figure 1.2'
- title 2: 'Distribution of Subjects by Age Category'
=========================================================================
Clean Up
=========================================================================
lib_sync: synchronized data in library 'sdtm'
lib_unload: library 'sdtm' unloaded
=========================================================================
Log End Time: 2021-11-20 23:06:57
Log Elapsed Time: 0 00:00:07
=========================================================================