-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic-features.Rmd
180 lines (129 loc) · 4.6 KB
/
basic-features.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
---
title: "GT Demo - basic features"
author: "John D. Smith"
date: "10/16/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Resources
- <https://blog.rstudio.com/2020/04/08/great-looking-tables-gt-0-2/>
- <https://gt.rstudio.com/>
- <https://gt.rstudio.com/reference/index.html>
# Is it really a *grammar*?
Tables are very complex and seem under-theorized (Wilkinson's Grammar of Graphics [book](https://isbn.nu/0387987746) really describes a **theory**). Whether we have a theory of tables might be debatable. There may be formal definitions of a grammar, but from a practical point of view, a grammar is internalized through use and hopefully becomes invisible when we *think in the grammar's language.*
Nice features of `gt` include:
- Ability to build tables by chaining functions with the `%>%` pipe
- An intuitive naming of functions starting with the following roots:
- `tab_`
- `fmt_`
- `cols_`
- `cels_`
- `opt_`
- `info_`
- Heavy reliance on `tidyverse` functions
A grammar is something you can learn (even if it's difficult) but becomes so intuitive that we become unaware of using it. That level of familiarity seems far off to me.
# Demo Set up
## Load libraries
```{r, message=FALSE}
suppressPackageStartupMessages(library(tidyverse))
library(gt)
```
This demo meanders from the outside and top of a table and works its way in.

## `gtcars` is a handy demo table that comes with the `gt` package
```{r}
glimpse(gtcars)
```
Right off the bat: a table with 15 columns and 47 rows would be too wide and too long to be useful. Start with a US subset.
```{r}
us_cars <- gtcars %>% filter(ctry_origin == "United States")
```
# The tour
In lieu of something really cool like [Gina Reynold's flipbook setup](https://evamaerey.github.io/ggplot_flipbook/ggplot_flipbook_xaringan.html#1), we can write and re-write a `gt` object called `t.`
## A default table
15 columns is still too wide, although `gt` tries to produce something:
```{r}
t <- us_cars %>%
gt()
t
```
## Tidy the data before passing it to `gt`
The tidyverse norm is to shape the data, select columns and order them before passing to a special-purpose sub-language like `gt`:
```{r}
t <- us_cars %>%
select(mfr, model, year, bdy_style, hp, msrp) %>%
gt()
t
```
That replaces the `t` object. The `gt` object `t` caries a list of columns with it. It's a list of lists that gets added to step-by-step, starting with:
```{r}
str(t, max.level = 1)
```
## Add a minimal title & subtitle
The *grammar* aspect of `gt` is reminiscent of `gglot2` and takes some effort to learn.
```{r}
t <- t %>%
tab_header(title = "Luxury US cars", subtitle = "basic characteristics")
t
```
## Add a source note at the bottom
Once you get the hang of it, you can remember the grammar or reason by analogy.
```{r}
t <- t %>%
tab_source_note("source: data included in the 'gt' package")
t
```
## Replace variable names with column labels
As we'll see, some `gt` functions take a raw column name while others require the `vars()` function.
```{r}
t <- t %>%
cols_label(
mfr = "Manufacturer",
year = "Year",
model = "Model",
bdy_style = "Style",
hp = "Horse Power",
msrp = "List Price"
)
t
```
`md()` and `html()` functions are the mechanisms for formatting text using either markdown or HTML notation, either at a column or table header level.
```{r}
t <- t %>%
cols_label(
mfr = md("**Manufacturer**"),
year = "Year",
model = "Model",
bdy_style = "Style",
hp = html("Horse<br>Power"),
msrp = html("List<br>Price")
)
t
```
## Reordering columns
The `cols_move_to_start` and related functions require that you specify the list of columns you are referring to using the tidyverse `dplyr` functions like `vars()`, `starts_with()`, `ends_with()`, `contains()`, `matches()`, `one_of()`, or `everything()`.
```{r}
t <- t %>%
cols_move_to_start(columns = vars(mfr, bdy_style))
t
```
## Formatting the data in a column
All the "`fmt_*` functions require the use of the `dplyr` helper functions:
- `fmt_currency()` - Format currency values
- `fmt_number()` - Format numeric values
- `fmt_scientific()` - Format values to scientific notation
- `fmt_percent()` - Format values as a percentage
- `fmt_date()` - Format values as dates
- `fmt_time()` - Format values as times
- `fmt_datetime()` - Format values as dates-times
```{r}
t <- t %>%
fmt_currency(columns = vars(msrp), currency = "USD", decimals = 0)
t
```
## Save the table as html, png, latex, or rtf
```{r}
gtsave(t, "basic-demo.html")
```