-
Notifications
You must be signed in to change notification settings - Fork 1
/
stable.qmd
408 lines (280 loc) · 8.98 KB
/
stable.qmd
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
---
title: stable
subtitle: Create a simple table from a data frame.
---
```{r, include = FALSE}
knitr::opts_chunk$set(comment = '.')
source("global.R")
```
## Syntax
Pass your data.frame into `stable()`
```{r, eval = FALSE}
stable(data)
```
Other formal arguments include:
- **align** to set column alignment
- **panel** to create groups of rows under a "panel" header
- **span** to group columns under a "spanner" header
- **notes** to create table notes
- **sumrows** to insert summary rows
- **units** that get placed below the corresponding column name
- **drop** to omit certain columns from the table
- **sizes** to set different table size attributes
- **escape_fun** a function to sanitize table items
You can also pass a bunch of other arguments through `...` to further format
the table (see `?stable` for details)
## Basics
`stable()` is the name of the workhorse function that is used to turn
data.frames into `TeX` tables. This chapter will introduce the `stable()`
function and how to us it to create basic tables.
To illustrate usage and features of `stable()`, we will use the `stdata` data
set that comes with pmtables
```{r}
data <- stdata()
head(data)
```
We can turn this data frame into a `TeX` table by passing it into `stable()`.
```{r}
out <- stable(data)
head(out, n = 10)
```
Note that we have shown the raw latex code that is generated by `stable()`. That
is to say: the output from `stable()` is a character vector of latex code
for the table. Note also that this character vector has a special class
associated with it: `stable`. That means we can write functions that recognize
this character vector as output from `stable()` and we can have those functions
process the character vector in special ways.
We can render that table in `TeX` **in the current Rmarkdown document** by
passing the text to `st_asis()`.
```{r}
out %>% st_as_image()
```
Remember to only call `st_asis()` when you are rendering tables inline in an
Rmd document. If you are sending table code to a `TeX` report, then
you will save them to a file and then include them into your report.
The remaining sections of this chapter will show you how to modify and
enhance this output in the more basic ways. We will implement separate
chapters for more complicated table manipulations.
## Annotate with file names
pmtables can track and annotate your table with the filenames of the
R code that generated the table (`r_file`) as well as the output file
where you write the the table `.tex` code (`output_file)`.
To have pmtables annotate your table with these file names, pass them
in with the `r_file` and `output_file` arguments
```{r}
out <- stable(
data,
r_file = "tables.R",
output_file = "tables.tex"
)
```
When we look at the rendered table, these names will show up as annotations
at the bottom of the table
```{r}
out %>% st_as_image()
```
## Saving your stable
Saving your stable **can** be as easy as sending it into `writeLines()`
```{r}
writeLines(
out,
con = tempfile(tmpdir = '.', fileext = ".tex")
)
```
But remember that we passed in the `output_file` argument to `stable()`
and we can use that data to save the table code to the file we named
in that argument.
Note that our `stable` object has another attribute now called `stable_file`
```{r}
attributes(out)
```
This has the value that we passed in as `output_file`. To save our table
to `stable_file`, we call `stable_save()`
```{r}
stable_save(out)
```
There is a `dir` argument to `stable_save()` that we can use to to select
the directory where the file will be saved
```{r}
stable_save(out, dir = tempdir())
```
And if you look at the default value for `dir` in `?stable_save`, you'll
see that this is associated with an option called `pmtables.dir`; you
can set that option to your default output directory and your tables
will be saved there until you change that
```{r, eval=FALSE}
options(pmtables.dir = tempdir())
stable_save(out)
```
```{r, include = FALSE}
options(pmtables.dir = NULL)
```
## Align columns
Use the `align` argument to align column data to the left, center or right. Use
a `cols_*` function to specify the default alignment for all columns
```{r}
tmp <- tibble(AB = 1, CDEFGHIJ = 2, KL = 3)
stable(tmp, align = cols_center()) %>% st_as_image()
```
You can pass in exceptions to the default
```{r}
stable(tmp, align = cols_center(CDEFGHIJ = "r")) %>%
st_as_image()
```
Or you can pass an alignment directive and the columns that are bound by that
directive
```{r}
stable(tmp, align = cols_center(.l = "AB,KL")) %>%
st_as_image()
```
A special directive called `.outer` lets you specify the alignment of the first
and last column in the table. For example, this code puts the first column to
the left and the last column to the right.
```{r}
stable(tmp, align = cols_center(.outer = "lr")) %>%
st_as_image()
```
### Fixed column widths
Use `col_ragged(size)` to force a column to be a fixed size.
```{r}
stable(tmp, align = cols_center(AB = col_ragged(2))) %>%
st_as_image()
```
By default, the unit is `cm` so that the first column (`AB`) has a width
of 2 cm regardless of the contents.
See `cols_align()` help topic for more information and argument descriptions.
## Manipulating columns and names
### Rename columns
You can change the name that appears in the rendered table with `cols_rename`
```{r}
data %>%
slice(1:3) %>%
stable(cols_rename = c(Age = "AGE", Weight = "WT")) %>%
st_as_image()
```
Note that the rename syntax follows the tidyselect convention of putting the
new name on the left and the old name on the right.
### Hide a column name
You can also "erase" the name of a column in the output
```{r}
data %>%
slice(1:3) %>%
stable(cols_blank = "WT,ALB,SCR") %>%
st_as_image()
```
### Don't print any table header information
```{r}
data %>%
slice(1:3) %>%
stable(cols_omit = TRUE) %>%
st_as_image()
```
### Unmask column names
In tibbles, you can't have duplicate column names. The `cols_split` argument
lets you unmask the names when duplicate names are prefixed with a tag and
a delimiter
```{r}
tmp <- tibble(a.A = 1, b.A = 2, c.A = 3)
```
```{r}
stable(tmp, cols_split = '.') %>%
st_as_image()
```
### Make column names bold
```{r}
data %>%
slice(1:2) %>%
stable(cols_bold = TRUE) %>%
st_as_image()
```
### Drop a column from the table
If we want to prevent a column from appearing in the output table (e.g.
`FORM`)
```{r}
head(data)
```
list the column name as `drop`
```{r}
stable(data, drop = "FORM") %>% st_as_image()
```
Of course some tidyverse could accomplish the same thing
```{r, eval = FALSE}
data %>% select(-FORM) %>% stable()
```
## Other customizations
### Notes
Arbitrary notes can get added to any table using the `notes` argument.
```{r}
data %>%
slice(1:3) %>%
stable(notes = "Showing just the first three rows") %>%
st_as_image()
```
The appearance of the notes can be controlled by calling `noteconf()` and
passing the result as `note_config`. See `?tab_notes()` for more details.
### Units
pmtables can automatically place units underneath the appropriate column. To
do this, generate a list with names that match the column names you want to
label with units.
```{r}
u <- list(
WT = "kg", CRCL = "ml/min", AGE = "year", ALB = "g/dL",
SCR = "mg\\%"
) %>% map(~paste0("(", .x, ")"))
```
Then pass that list as `units` to `stable()`
```{r}
stable(data, units = u) %>% st_as_image()
```
### Multi-line column headers
If the column header is long, you can break it across multiple lines. By
default, use `...` in the column name
```{r}
tibble(`First line ... Second line` = 123456789) %>%
stable() %>%
st_as_image()
```
The break can be introduced through the rename mechanism
```{r}
tibble(a = 1) %>%
stable(cols_rename = c(`First ... Second` = "a")) %>%
st_as_image()
```
Look at the `?tab_cols` help topic for the `cols_break` argument; this
lets you change the character sequence used for the break.
### Insert horizontal lines
Pass `hlines_at` to insert horizontal lines above specific rows. This can
be either logical vector with the same length as the number of rows in the
table or a vector of integers.
```{r}
stable(stdata(), hline_at = c(3,5)) %>%
st_as_image()
```
or
```{r}
stable(stdata(), hline_at = data$FORM == "tablet") %>% st_as_image()
```
Pass `hlines_from` to derive hline locations based on non-repeating values
in a table column. Notice how this behaves.
```{r}
stable(stdata(), hline_from = "DOSE") %>%
st_as_image()
```
See the `?tab_hlines` help topic for more info. See also `st_hline()` for
the pipe equivalent with additional feature.
### Clear replicate values
You can create groups in a table by "clearing" replicate values
```{r}
stable(stdata(), clear_reps = "STUDY") %>%
st_as_image()
```
This can be combined with an hline
```{r}
stable(
stdata(),
clear_reps = "STUDY",
hline_from = "STUDY"
) %>% st_as_image()
```
See `?tab_clear_reps` for other options, including an option for clearing
based on several grouping variables.