-
Notifications
You must be signed in to change notification settings - Fork 11
/
04-tables-building.Rmd
64 lines (46 loc) · 2.03 KB
/
04-tables-building.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
# Tables
In this section, we use the `Tplyr` package to build a demographics table using a subject-level dataset. Exporting tables using the `officer` package is also covered.
## Building
### Dataset Source
For demonstration purposes, we will use the `adsl` from the <a href="https://advance.phuse.global/display/WEL/Test+Dataset+Factory">PHUSE Test Datasets Factory</a> repository.
```{r, message = F, warning = F, error = F}
library(haven)
adsl <- read_xpt("https://github.com/phuse-org/TestDataFactory/raw/main/Updated/TDF_ADaM/adsl.xpt")
```
### String Formatting
This section sets the default formats for tables built with Tplyr.
For each variable type (continuous, categorical), we specify the following:
1. Which summary statistic(s) should be computed?
2. How should the summary statistic(s) be formatted for display?
Once set, these formats are carried over to your table automatically.
```{r, message = F, warning = F}
library(Tplyr)
options(
# Categorical variable defaults
tplyr.count_layer_default_formats =
list(n_counts = f_str("xxx [xx.xx%]", n, pct)),
# Continuous variable defaults
tplyr.desc_layer_default_formats =
list('N' = f_str('xx', n),
'Mean [SD]' = f_str('xx.xx [xx.xxx]', mean, sd),
'Median' = f_str('xx.x', median),
'Min, Max' = f_str('xx, xx', min, max))
)
```
### Create
For demonstration purposes, we will select a handful of variables from the adsl dataset to work with.
```{r, warning = F, message = F}
library(dplyr)
# Initiate Tplyr, specify treatment variable, optional where condition
my_table <- tplyr_table(adsl, TRT01P, where = SAFFL == "Y") %>%
# Add a total group column
add_total_group() %>%
# Add individual variables here
add_layer(group_desc(AGE, b = "Age (years)")) %>%
add_layer(group_count(AGEGR1, b = "Age Group 1 (years)")) %>%
add_layer(group_count(SEX, b = "Gender")) %>%
add_layer(group_count(ETHNIC, b = "Ethnicity")) %>%
add_layer(group_desc(BMIBL, b = "Baseline Body Mass Index (kg/m2)")) %>%
# Build
build()
```