-
Notifications
You must be signed in to change notification settings - Fork 24
/
04z-PRACTICE-03-04.qmd
139 lines (110 loc) · 4.2 KB
/
04z-PRACTICE-03-04.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
---
title: "Practice with Modules 03 and 04"
format: html
html-table-processing: none
---
```{r}
library(gt)
library(tidyverse)
```
Practice time again. Here we can experiment with the `fmt_*()` and `cols_*()` functions.
### **gt** functions from module `03`
- `fmt_number()`
- `fmt_integer()`
- `fmt_scientific()`
- `fmt_engineering()`
- `fmt_percent()`
- `fmt_currency()`
- `fmt_date()`
- `fmt_time()`
- `fmt_datetime()`
- `fmt_markdown()`
- `fmt_missing()`
- `data_color()`
- `info_locales()`
- `info_currencies()`
- `info_date_style()`
- `info_time_style()`
- `info_paletteer()`
### From module `04`
- `cols_align()`
- `cols_label()`
- `cols_width()`
- `cols_move()`
- `cols_hide()`
- `cols_merge_range()`
- `cols_merge_uncert()`
- `cols_merge_n_pct()`
- `cols_merge()`
The formatting functions (`fmt_*()` + `data_color()`), information functions (`info_*()`), and functions that work with columns (`cols_*()`) were looked at. Below are a few examples for practicing.
#### Suggestions for practice
The `fmt_number()` function allows us to format numbers. This is important as tables often contain number with specific formatting requirements. There are many options for formatting numbers. One suggestion for working with `fmt_number()` is to modify the options given to it. Working with just the `num` column from `exibble` and exposing all of the function's default options, go ahead and change things and re-run the code.
```{r}
exibble |>
dplyr::select(num) |>
gt() |>
fmt_number(
columns = num,
rows = everything(),
decimals = 2,
n_sigfig = NULL,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
scale_by = 1,
suffixing = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
locale = NULL
)
```
The `data_color()` function allows us to add color to cells. Try using different `method` values (`"numeric"`, `"bin"`, `"quantile"`, etc.), change the `palette`, and try using a non-`NULL` `domain` (hint: should be a vector of length 2). After this, try using a `fmt_*()` function that nicely formats these population values.
```{r}
countrypops |>
dplyr::filter(country_name == "United States") |>
dplyr::select(-contains("code")) |>
tail(10) |>
gt(rowname_col = "year", groupname_col = "country_name") |>
tab_options(column_labels.hidden = TRUE) |>
data_color(
columns = population,
method = "numeric",
palette = c("black", "orange", "blue", "green"),
domain = NULL
)
```
Setting column widths can be admittedly confusing. It uses browser logic to great extent and there are really a lot of possible outcomes. Try combinations of setting the overall width (`tab_options(table.width = ...)`) and using the `pct()` and `px()` helpers to define percentage and pixel widths. These interactions are occasionally surprising but there is some helpful documentation on all the interactions at https://github.com/rstudio/gt/blob/master/tests/gt-examples/01-html-script/html-17-table_width_specifications.R.
```{r}
exibble |>
dplyr::select(num, char, date, datetime, row) |>
gt() |>
tab_options(table.width = pct(100)) |>
cols_width(
num ~ pct(20),
ends_with("r") ~ px(100),
starts_with("date") ~ px(200),
everything() ~ pct(30)
)
```
Lastly, some information might do us good. The information functions (`info_*()`) can provide us with a helpful reference when we need it most.
Many `fmt_*()` functions have a `locale` option for locale-specific formatting. Here is information on which locales are supported:
```{r}
info_locales()
```
The `fmt_currency()` function has support for many currencies through its `currency` option. Here is information on which currencies are supported, their codes, and a preview of their formatting:
```{r}
info_currencies()
```
The `fmt_date()`, `fmt_time()`, and `fmt_datetime()` functions use numbered styles as a shortcut for specifying date and time styles. A reference for those can be accessed with:
```{r}
info_date_style()
```
and
```{r}
info_time_style()
```
You may want to use a color palette with `data_color()`. Pretty much every color palette package in R is contained in the **paletteer** package. This info function shows them all in a **gt** table:
```{r}
info_paletteer()
```