-
Notifications
You must be signed in to change notification settings - Fork 11
/
05-tables-formatting.Rmd
117 lines (84 loc) · 3.3 KB
/
05-tables-formatting.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
## Formatting
### Post Processing
By default, Tplyr will build your table and store it as a tibble. It can be accessed directly in your environment by simply calling it.
```{r}
head(my_table, n = 9)
```
Since it is tibble, you are free to further process or wrangle it to your liking. Below we'll apply some light formatting using a combination of `Tplyr` and `dplyr`:
1. Remove repeating variable labels and insert breaks between variable rows
2. Reorder the columns
3. Remove extraneous columns not of interest for display (i.e. meta data created by Tplyr)
```{r, message = F, warning = F}
library(dplyr)
my_table <- my_table %>%
# remove repeating labels
apply_row_masks(., row_breaks = TRUE) %>%
# specify order of relevant variables
select(row_label1,
row_label2,
`var1_Xanomeline High Dose`,
`var1_Xanomeline Low Dose`,
var1_Placebo,
var1_Total)
```
We can see the formatting has taken effect by recalling it in our environment. Specifically, compare the `row_label1` variable below to the previous section while also noting the order of columns has changed to our specification.
```{r}
head(my_table, n = 9)
```
### Displaying
While `Tplyr` aided us in *building* a summary tables, other packages exist to help us *display* them. One package we will focus on is the `flextable` package. This table display package is quite powerful and offers a high degree of customization.
We turn the interested reader to the <a href="https://ardata-fr.github.io/flextable-book/"> flextable book</a> to view additional examples.
```{r, warning = F, message = F}
library(flextable)
# a basic flextable
my_flextable <- my_table %>%
# start flextable
flextable() %>%
autofit()
```
```{r}
my_flextable
```
We can apply some `flextable` formatting to alter the display of the table.
```{r}
# a nicer flextable
my_flextable <- my_table %>%
# start flextable
flextable() %>%
autofit() %>%
# add some padding between rows
padding(padding = 0.5) %>%
# adjust width of first two columns
width(j = 1:2, width = 4) %>%
# align treatment columns to center
align(part = "all", align = "center", j = 3:6) %>%
# column header labels
set_header_labels(., values = list(
row_label1 = 'Variable',
row_label2 = ' ',
`var1_Xanomeline High Dose` = 'Xanomeline \nHigh Dose',
`var1_Xanomeline Low Dose` = 'Xanomeline \nLow Dose',
var1_Placebo = 'Placebo',
var1_Total = 'Total')) %>%
# header + footers
add_header_lines(values = "Table: Demographics (Safety Analysis Set)") %>%
add_footer_lines(values = "This was produced in R!") %>%
# font size, font name
fontsize(part = "all", size = 8)
# font()
# font(part = "all", fontname = "Times")
# This errors, perhaps version issue. -- SZ
```
```{r echo = F}
my_flextable
```
## Exporting
To export our table to MS Word (.docx), we may use the `officer` package. This package simplifies working with various Microsoft Office formats and integrates nicely with `flextable`.
```{r, warning = F, message = F, eval = F}
library(officer)
# send the flextable to .docx output
read_docx() %>%
body_add_flextable(my_flextable) %>%
print(target = "my_document.docx")
```
In your working directory, you should now see the file `my_document.docx` has been created and contains your table.