forked from rich-iannone/gt-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-create-table.Rmd
229 lines (161 loc) · 6.88 KB
/
01-create-table.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
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
---
title: "Creating a Table, Exporting"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gt)
library(tidyverse)
```
## Intro
All **gt** tables begin with a call to `gt()`, where we would supply the input data table (data frame or tibble) and some basic options for creating a stub (`rowname_col`) and row groups (`groupname_col`). We can also introduce a grouped tibble (via **dplyr**’s `group_by()`) for more precise divisions of rows into row groups.
The `gt_preview()` function is great for getting a nicely-formatted preview of a data table (defaulting to the first 5 rows and the last row).
Finally, sometimes we want to export the table to some format. It could be HTML, it could be RTF... whichever you go with, you can use the `gtsave()` function.
------
### Functions in this module
- `gt()`
- `gt_preview()`
- `gtsave()`
------
### `gt()`: Create a **gt** table object
``` r
gt(
data,
rowname_col = "rowname",
groupname_col = dplyr::group_vars(data),
caption = NULL,
rownames_to_stub = FALSE,
auto_align = TRUE,
id = NULL,
row_group.sep = getOption("gt.row_group.sep", " - ")
)
```
The `gt()` function creates a **gt** table object when provided with table data. Using this function is the first step in a typical **gt** workflow. Once we have the **gt** table object, we can perform styling transformations before rendering to a display table of various formats.
##### EXAMPLES
The **gt** package contains a few datasets:
- `countrypops` - Yearly populations of countries from 1960 to 2017
- `sza` - Twice hourly solar zenith angles by month & latitude
- `gtcars` - Deluxe automobiles from the 2014-2017 period
- `sp500` - Daily S&P 500 Index data from 1950 to 2015
- `pizzaplace` - A year of pizza sales from a pizza place
- `exibble` - A toy example tibble for testing with **gt**: `exibble`
We will use `exibble` *a lot* during this workshop simply because the entire table easily fits on a screen and contains various types of data. It's perfect for examples.
Let's print out `exibble` and see what it looks like:
```{r}
exibble
```
Now lets introduce `exibble` to the `gt()` function.
```{r}
exibble %>% gt()
```
The above is the basic layout, containing the column labels and the body cells. Numbers are minimally formatted (see the `num` column) but we can apply our own specific formatting (in the next module).
We can modify the layout a bit in the first step and create a stub. A stub is a special column that contains row labels/names. To do this, we supply the name of the column we'd like to use as the stub to the `rowname_col` argument. Conveniently, we have the `"row"` column in `exibble` that's good for this purpose:
```{r}
exibble %>% gt(rowname_col = "row")
```
Great! A bit on the *stub*. It doesn't have a column label above it because it's technically not a column (it's the... stub). We can still put a label above it though! That's with the `tab_stubhead()` function (in the next module).
We can do some more structuring. Sets of rows can be grouped, showing a *row group label* above each group. We can do this by using a column containing categorical values (usually grouping labels). Conveniently again, exibble has the `"group"` column. Let's use that in the `rowname_col` argument of `gt()` and see what it looks like:
```{r}
exibble %>% gt(rowname_col = "row", groupname_col = "group")
```
Even if rows are in a weird order, **gt** will put the rows in the correct group (but still respect the order of rows).
```{r}
exibble %>% dplyr::sample_n(size = 8) %>% gt(rowname_col = "row", groupname_col = "group")
```
Say you have a data frame with row names (like `mtcars`). You can put those row.names in the stub with `rownames_to_stub = TRUE`:
```{r}
mtcars %>%
dplyr::slice_head(n = 10) %>%
gt(rownames_to_stub = TRUE)
```
But if you're into **dplyr** you're probably into `group_by()`-ing and all that jazz. If you have a grouped tibble, you can pass that into `gt()` and it'll know what to do:
```{r}
exibble %>%
dplyr::group_by(group) %>%
gt(rowname_col = "row")
```
This also works if you have multiple columns grouped. Use `row_group.sep` to specify the separator between labels.
```{r}
pizzaplace %>%
dplyr::filter(type %in% c("supreme", "veggie")) %>%
dplyr::group_by(type, size) %>%
dplyr::slice_head(n = 3) %>%
gt(row_group.sep = " // ")
```
------
### `gt_preview()`: Preview a **gt** table object
``` r
gt_preview(
data,
top_n = 5,
bottom_n = 1,
incl_rownums = TRUE
)
```
Sometimes you may want to see just a small portion of your input data. We can use `gt_preview()` in place of `gt()` to get the first x rows of data and the last y rows of data (which can be set by the `top_n` and `bottom_n` arguments).
##### EXAMPLES
Use `gtcars` to create a **gt** table preview (with only a few of its columns). You'll see the first five rows and the last row.
```{r}
gtcars %>%
dplyr::select(mfr, model, year) %>%
gt_preview()
```
Don't want to see the row numbers? That's fine, use `incl_rownums = FALSE`.
```{r}
sp500 %>%
gt_preview(
bottom_n = 5,
incl_rownums = FALSE
)
```
------
### `gtsave()`: Save a **gt** table as a file
``` r
gtsave(
data,
filename,
path = NULL,
...
)
```
The `gtsave()` function makes it easy to save a **gt** table to a file. The function assumes the output file type by the extension provided in the output filename. This will produce either an HTML, PDF, PNG, LaTeX, or RTF file.
##### EXAMPLES
Use `exibble` to create a **gt** table.
```{r}
gt_tbl <-
exibble %>%
gt(
rowname_col = "row",
groupname_col = "group"
)
```
Get an HTML file with inlined CSS (which is necessary for including the table as part of an HTML email in the **blastula** package).
```{r}
#gt_tbl %>% gtsave("tab_1_inlined.html", inline_css = TRUE)
```
By leaving out the `inline_css` option, we get a more conventional HTML file with embedded CSS styles.
```{r}
#gt_tbl %>% gtsave("tab_1.html")
```
Get an RTF file by using the .rtf extension in the `filename`.
```{r}
#gt_tbl %>% gtsave("tab_r.rtf")
```
Get a PNG file (essentially a screenshot of the HTML table) by using the .png extension.
```{r}
#gt_tbl %>% gtsave("tab_r.png")
```
Get a LaTeX table fragment by using the .ltx extension.
```{r}
#gt_tbl %>% gtsave("tab_r.ltx")
```
If you know about LaTeX, you'll know that you generally need LaTeX packages and `gt_latex_dependencies()` gives us the names of the packages that **gt** needs.
```{r}
#gt::gt_latex_dependencies() %>% cat()
```
------
### SUMMARY
1. Want to make a **gt** table? Get your input table in the right form and use `gt()`.
2. The `gt()` function has a few options for adding group rows and a *stub*.
3. If you need a quick preview of a table (regardless of the table's size) try `gt_preview()`
4. You can save a **gt** table in a file with `gtsave()`; trick is to name the file in a supported format (e.g., `"gt_table.rtf"` for an RTF file containing the table).