forked from elong0527/r4csr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlf-baseline.Rmd
123 lines (99 loc) · 3.45 KB
/
tlf-baseline.Rmd
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
# Baseline characteristics
Following [ICH E3 guidance](https://database.ich.org/sites/default/files/E3_Guideline.pdf),
we need to summarize critical demographic and baseline characteristics of the participants
in Section 11.2, Demographic and Other Baseline Characteristics.
In this chapter, we illustrate how to create a simplified
baseline characteristics table for a study.
```{r, include=FALSE}
source("common.R")
```
```{r, out.width = "100%", out.height = "400px", echo = FALSE, fig.align = "center"}
knitr::include_graphics("tlf/tlf_base.pdf")
```
There are many R packages that can efficiently summarize baseline information.
The [`table1`](https://github.com/benjaminrich/table1) R package is one of them.
```{r}
library(table1)
library(r2rtf)
library(haven)
library(dplyr)
library(tidyr)
library(stringr)
library(tools)
```
As in previous chapters, we first read the `adsl` dataset that contains all
the required information for the baseline characteristics table.
```{r}
adsl <- read_sas("data-adam/adsl.sas7bdat")
```
For simplicity, we only analyze `SEX`, `AGE` and, `RACE` in this example
using the `table1` R package. More details of the `table1` R package can
be found in the package
[vignettes](https://benjaminrich.github.io/table1/vignettes/table1-examples.html).
The `table1` R package directly creates an HTML report.
```{r}
ana <- adsl %>%
mutate(
SEX = factor(SEX, c("F", "M"), c("Female", "Male")),
RACE = toTitleCase(tolower(RACE))
)
tbl <- table1(~ SEX + AGE + RACE | TRT01P, data = ana)
tbl
```
The code below transfer the output into a dataframe
that only contains ASCII characters
recommended by regulatory agencies.
`tbl_base` is used as input for `r2rtf` to create the final report.
```{r}
tbl_base <- tbl %>%
as.data.frame() %>%
as_tibble() %>%
mutate(across(
everything(),
~ str_replace_all(.x, intToUtf8(160), " ")
))
names(tbl_base) <- str_replace_all(names(tbl_base), intToUtf8(160), " ")
tbl_base
```
We define the format of the output. We highlight items that
are not discussed in previous discussion.
`text_indent_first` and `text_indent_left` are used to control the indent
space of text. They are helpful when you need to control the white space
of a long phrase, "AMERICAN INDIAN OR ALASKA NATIVE"
in the table provides an example.
```{r}
colheader1 <- paste(names(tbl_base), collapse = "|")
colheader2 <- paste(tbl_base[1, ], collapse = "|")
rel_width <- c(2.5, rep(1, 4))
tbl_base[-1, ] %>%
rtf_title(
"Baseline Characteristics of Participants",
"(All Participants Randomized)"
) %>%
rtf_colheader(colheader1,
col_rel_width = rel_width
) %>%
rtf_colheader(colheader2,
border_top = "",
col_rel_width = rel_width
) %>%
rtf_body(
col_rel_width = rel_width,
text_justification = c("l", rep("c", 4)),
text_indent_first = -240,
text_indent_left = 180
) %>%
rtf_encode() %>%
write_rtf("tlf/tlf_base.rtf")
```
```{r, include=FALSE}
rtf2pdf("tlf/tlf_base.rtf")
```
```{r, out.width = "100%", out.height = "400px", echo = FALSE, fig.align = "center"}
knitr::include_graphics("tlf/tlf_base.pdf")
```
In conclusion, the procedure to generate demographic and baseline characteristics table is summarized as follows:
- Step 1: Read the data set.
- Step 2: Use `table1::table1()` to get the baseline characteristics table.
- Step 3: Transfer the output from Step 2 into a data frame that only contains ASCII characters.
- Step 4: Define the format of the RTF table by using the R package `r2rtf`.