-
Notifications
You must be signed in to change notification settings - Fork 24
/
02z-PRACTICE-01-02.qmd
88 lines (73 loc) · 2.26 KB
/
02z-PRACTICE-01-02.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
---
title: "Practice with Modules 01 and 02"
format: html
html-table-processing: none
---
```{r}
library(gt)
library(tidyverse)
```
Practice is good. We learned a great number of functions in modules `01` and `02`. Here they are as a list:
### **gt** functions from module `01`
- `gt()`
- `gt_preview()`
- `gtsave()`
### From module `02`
- `tab_stubhead()`
- `tab_spanner()`
- `tab_header()`
- `tab_source_note()`
- `tab_footnote()`
- `tab_style()`
- `tab_options()`
- `contains()`
- `matches()`
- `starts_with()`
- `ends_with()`
- `everything()`
- `md()`
- `html()`
- `cells_title()`
- `cells_stubhead()`
- `cells_column_spanners()`
- `cells_column_labels()`
- `cells_row_groups()`
- `cells_stub()`
- `cells_body()`
- `cells_summary()`
- `cells_grand_summary()`
- `cell_text()`
- `cell_fill()`
- `cell_borders()`
Although this is a lot, in a short amount of time we can still get useful practice by trying a few things out.
#### Suggestions for practice
Try modifying this statement several times and see what happens (e.g., leave out the first argument, change values, etc.).
```{r}
exibble |> gt(rowname_col = "row", groupname_col = "group")
```
Try adapting this snippet of code to make a great set of `title` and `subtitle`. Use `md()` to get Markdown in there. Know HTML? Try your hand at some HTML titling with the `html()` helper. Use `gtsave()` to export the table to your preferred output type.
```{r}
gtcars |>
dplyr::select(mfr, model, msrp) |>
dplyr::slice(1:5) |>
gt() |>
tab_header(
title = "<title>",
subtitle = "<subtitle>"
)
```
To add styles in a **gt** table, we need to use the combination of `tab_style()`, one or more location helper functions (e.g,. `cells_body()`, `cells_column_labels()`, etc.), and a styling function like `cell_fill()`. Here's an example table with a number of different location types. Try changing the background of different locations.
```{r}
exibble |>
gt(rowname_col = "row", groupname_col = "group") |>
tab_header(title = "The title", subtitle = "The subtitle") |>
tab_source_note("A source note.") |>
tab_footnote(
footnote = "A footnote",
locations = cells_body(columns = 1, rows = 1)
) |>
tab_style(
style = cell_fill(color = "#E5873A"),
locations = cells_body(columns = char, rows = "row_1")
)
```