-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy path03-format-data.qmd
740 lines (583 loc) · 18.7 KB
/
03-format-data.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
---
title: "Formatting Data"
format: html
html-table-processing: none
---
```{r}
library(gt)
library(tidyverse)
```
## Intro
Columns of data can be formatted with the `fmt_*()` functions. We can specify the rows of these columns quite precisely with the `rows` argument. We get to apply these functions exactly once to each data cell (last call wins). Need to do custom formatting? Use the `fmt()` function and define your own formatting function (or, create a wrapper with `fmt()` if you prefer). The `text_transform()` function allows for post-processing of any data, and we provide a function for that transformation.
------
### Functions in this module
- `fmt_number()`
- `fmt_integer()`
- `fmt_scientific()`
- `fmt_percent()`
- `fmt_currency()`
- `fmt_date()`
- `fmt_time()`
- `fmt_datetime()`
- `fmt_markdown()`
- `sub_missing()`
- `data_color()`
- `text_transform()`
Information functions:
- `info_locales()`
- `info_currencies()`
- `info_date_style()`
- `info_time_style()`
- `info_paletteer()`
------
### `fmt_number()`: Format numeric values
```r
fmt_number(
data,
columns = everything(),
rows = everything(),
decimals = 2,
n_sigfig = NULL,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
accounting = FALSE,
scale_by = 1,
suffixing = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
system = c("intl", "ind"),
locale = NULL
)
```
With numeric values in a **gt** table, we can perform number-based formatting so that the targeted values are rendered with a higher consideration for tabular presentation.
We get a lot of control over numeric formatting with the following options:
- decimals: choice of the number of decimal places, option to drop trailing zeros, and a choice of the decimal symbol
- digit grouping separators: options to enable/disable digit separators and provide a choice of separator symbol
- scaling: we can choose to scale targeted values by a multiplier value
- large-number suffixing: larger figures (thousands, millions, etc.) can be autoscaled and decorated with the appropriate suffixes
- pattern: option to use a text pattern for decoration of the formatted values
- locale-based formatting: providing a locale ID (e.g., `"de"`) will result in number formatting specific to the chosen locale
##### EXAMPLES
Use `exibble` to create a **gt** table. Format the `num` column using `fmt_number()` and its default options.
```{r}
exibble |>
dplyr::select(num, char) |>
gt() |>
fmt_number()
```
We can limit which numeric columns are formatted by targeting columns to format in the `columns` argument.
```{r}
exibble |>
dplyr::select(num, char, currency) |>
gt() |>
fmt_number(columns = num)
```
Let's format the `num` column as numeric with three decimal places. Don't use digit separators (`use_seps = FALSE`).
```{r}
exibble |>
dplyr::select(num) |>
gt() |>
fmt_number(
decimals = 3,
use_seps = FALSE
)
```
Format only the first three rows of `num`, this time with 4 decimal places.
```{r}
exibble |>
dplyr::select(num) |>
gt() |>
fmt_number(
rows = 1:3,
decimals = 4
)
```
Format only the rows of `num` (to 4 decimal places) where values in `currency` are greater than 30.
```{r}
exibble |>
dplyr::select(num, currency) |>
gt() |>
fmt_number(
columns = num,
rows = currency > 30,
decimals = 4
)
```
Combine the conditional selection of rows with scaling values by 1/1000 and then putting a `"K"` after the scaled values (with a `pattern`).
```{r}
exibble |>
dplyr::select(num) |>
gt() |>
fmt_number(
columns = num,
rows = num > 500,
decimals = 1,
scale_by = 1/1000,
pattern = "{x}K"
)
```
Use `countrypops` to create a **gt** table. Format all numeric columns to use large-number suffixing (with `suffixing = TRUE`).
```{r}
countrypops |>
dplyr::select(country_code_3, year, population) |>
dplyr::filter(country_code_3 %in% c("CHN", "IND", "USA", "PAK", "IDN")) |>
dplyr::filter(year > 1975 & year %% 5 == 0) |>
tidyr::spread(year, population) |>
dplyr::arrange(desc(`2015`)) |>
gt(rowname_col = "country_code_3") |>
fmt_number(
decimals = 2,
suffixing = TRUE
)
```
With `exibble`, make a simple **gt** table. Format the `num` column as numeric with three decimal places and do this formatting according to the `"fr"` locale.
```{r}
exibble |>
dplyr::select(num) |>
gt() |>
fmt_number(
columns = num,
decimals = 3,
locale = "fr" # <- "fr_FR", "fr_CA", "de", "de_AT"
)
```
------
### `fmt_integer()`: Format values as integers
```r
fmt_integer(
data,
columns = everything(),
rows = everything(),
use_seps = TRUE,
accounting = FALSE,
scale_by = 1,
suffixing = FALSE,
pattern = "{x}",
sep_mark = ",",
force_sign = FALSE,
system = c("intl", "ind"),
locale = NULL
)
```
With numeric values in a **gt** table, we can perform number-based formatting so that the targeted values are always rendered as integer values.
##### EXAMPLE
Use `exibble` to create a **gt** table. Format the `num` column using `fmt_integer()` and its default options.
```{r}
exibble |>
dplyr::select(num) |>
gt() |>
fmt_integer()
```
------
### `fmt_scientific()`: Format values to scientific notation
```r
fmt_scientific(
data,
columns = everything(),
rows = everything(),
decimals = 2,
drop_trailing_zeros = FALSE,
scale_by = 1,
exp_style = "x10n",
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign_m = FALSE,
force_sign_n = FALSE,
locale = NULL
)
```
With numeric values in a **gt** table, we can perform formatting so that the targeted values are rendered in scientific notation.
##### EXAMPLE
Use `exibble` to create a **gt** table. Format the `num` column as partially in scientific notation (values <=500).
```{r}
exibble |>
dplyr::select(num, currency) |>
gt() |>
fmt_scientific(
columns = num,
rows = num <= 500,
decimals = 1
)
```
------
### `fmt_percent()`: Format values as a percentage
```r
fmt_percent(
data,
columns = everything(),
rows = everything(),
decimals = 2,
drop_trailing_zeros = FALSE,
drop_trailing_dec_mark = TRUE,
scale_values = TRUE,
use_seps = TRUE,
accounting = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
incl_space = FALSE,
placement = "right",
system = c("intl", "ind"),
locale = NULL
)
```
With numeric values in a **gt** table, we can perform percentage-based formatting. It is assumed the input numeric values are in a fractional format since the numbers will be automatically multiplied by `100` before decorating with a percent sign. For the other scenario, where values just need a percent sign, use `scale_values = FALSE`.
##### EXAMPLE
Use `pizzaplace` to create a **gt** table. Format the `frac_of_quota` column to display values as percentages.
```{r}
pizzaplace |>
dplyr::mutate(month = as.numeric(substr(date, 6, 7))) |>
dplyr::group_by(month) |>
dplyr::summarize(pizzas_sold = dplyr::n()) |>
dplyr::ungroup() |>
dplyr::mutate(frac_of_quota = pizzas_sold / 4000) |>
gt(rowname_col = "month") |>
fmt_percent(
columns = frac_of_quota,
decimals = 1
)
```
------
### `fmt_currency()`: Format values as currencies
```r
fmt_currency(
data,
columns = everything(),
rows = everything(),
currency = "USD",
use_subunits = TRUE,
decimals = NULL,
drop_trailing_dec_mark = TRUE,
use_seps = TRUE,
accounting = FALSE,
scale_by = 1,
suffixing = FALSE,
pattern = "{x}",
sep_mark = ",",
dec_mark = ".",
force_sign = FALSE,
placement = "left",
incl_space = FALSE,
system = c("intl", "ind"),
locale = NULL
)
```
With numeric values in a **gt** table, we can perform currency-based formatting. This function supports both automatic formatting with a three-letter or numeric currency code. We can also specify a custom currency that is formatted according to the output context with the `currency()` helper function. Numeric formatting facilitated through the use of a locale ID.
##### EXAMPLES
Use `exibble` to create a **gt** table. Format the `currency` column to have currency values in euros (`EUR`).
```{r}
exibble |>
gt() |>
fmt_currency(
columns = currency,
currency = "EUR"
)
```
Use `exibble` to create a **gt** table. Keep only the `num` and `currency`, columns, then, format those columns using the `JPY` and `GBP` currencies.
```{r}
exibble |>
dplyr::select(num, currency) |>
gt() |>
fmt_currency(
columns = num,
currency = "JPY"
) |>
fmt_currency(
columns = currency,
currency = "GBP"
)
```
------
### `fmt_date()`: Format values as dates
```r
fmt_date(
data,
columns = everything(),
rows = everything(),
date_style = "iso",
pattern = "{x}",
locale = NULL
)
```
Format input date values that are either of the `Date` type, or, are character-based and expressed according to the ISO 8601 date format (`YYYY-MM-DD`). Once the appropriate data cells are targeted with columns (and, optionally, rows), we can simply apply a preset date style (see table in `info_date_style()` for info) to format the dates.
##### EXAMPLES
Use `exibble` to create a **gt** table. Keep only the `date` and `time` columns. Format the `date` column to have dates formatted as `month_day_year` (date style `5`).
```{r}
exibble |>
dplyr::select(date, time) |>
gt() |>
fmt_date(
columns = date,
date_style = "month_day_year"
)
```
Information on these formats are available in `info_date_style()`
```{r}
info_date_style()
```
Use `exibble` to create a **gt** table. keep only the `date` and `time` columns. Format the `date` column to have mixed date formats (dates after April will be different than the others).
```{r}
exibble |>
dplyr::select(date, time) |>
gt() |>
fmt_date(
columns = date,
rows = as.Date(date) > as.Date("2015-04-01"),
date_style = "m_day_year"
) |>
fmt_date(
columns = date,
rows = as.Date(date) <= as.Date("2015-04-01"),
date_style = "day_m_year"
)
```
------
### `fmt_time()`: Format values as times
```r
fmt_time(
data,
columns = everything(),
rows = everything(),
time_style = "iso",
pattern = "{x}",
locale = NULL
)
```
Format input time values that are character-based and expressed according to the ISO 8601 time format (`HH:MM:SS`). Once the appropriate data cells are targeted with columns (and, optionally, rows), we can simply apply a preset time style (see table in `info_time_style()` for info) to format the times.
##### EXAMPLES
Use `exibble` to create a **gt** table. Keep only the `date` and `time` columns. Format the `time` column to have times formatted as `"h_m_s_p"` (time style `3`).
```{r}
exibble |>
dplyr::select(date, time) |>
gt() |>
fmt_time(
columns = time,
time_style = "h_m_s_p"
)
```
Information on these formats are available in `info_time_style()`
```{r}
info_time_style()
```
Use `exibble` to create a **gt** table. Keep only the `date` and `time` columns. Format the `time` column to have mixed time formats (times after 16:00 will be different than the others).
```{r}
exibble |>
dplyr::select(date, time) |>
gt() |>
fmt_time(
columns = time,
rows = time > "16:00",
time_style = "h_m_s_p"
) |>
fmt_time(
columns = time,
rows = time <= "16:00",
time_style = "h_m_p"
)
```
------
### `fmt_datetime()`: Format values as date-times
```r
fmt_datetime(
data,
columns = everything(),
rows = everything(),
date_style = "iso",
time_style = "iso",
sep = " ",
format = NULL,
tz = NULL,
pattern = "{x}",
locale = NULL
)
```
Format input date-time values that are character-based and expressed according to the ISO 8601 date-time format (`YYYY-MM-DD HH:MM:SS`). Once the appropriate data cells are targeted with columns (and, optionally, rows), we can simply apply preset date and time styles (see tables in `info_date_style()` and `info_time_style()` for more info) to format the data-time values.
##### EXAMPLE
Use `exibble` to create a **gt** table. keep only the `datetime` column. Format the column to have dates formatted as `month_day_year` and times to be `"h_m_s_p"`.
```{r}
exibble |>
dplyr::select(datetime) |>
gt() |>
fmt_datetime(
columns = datetime,
date_style = "month_day_year",
time_style = "h_m_s_p"
)
```
------
### `fmt_markdown()`: Format Markdown text
```r
fmt_markdown(
data,
columns = everything(),
rows = everything(),
md_engine = c("markdown", "commonmark")
)
```
Any Markdown-formatted text in the incoming cells will be transformed to the appropriate output type during render when using `fmt_markdown()`.
##### EXAMPLE
Create a few Markdown-based text snippets.
```{r}
text_1a <- "
### This is Markdown.
Markdown’s syntax is comprised entirely of
punctuation characters, which punctuation
characters have been carefully chosen so as
to look like what they mean... assuming
you’ve ever used email.
"
text_1b <- "
Info on Markdown syntax can be found
[here](https://daringfireball.net/projects/markdown/).
"
text_2a <- "
The **gt** package has these datasets:
- `countrypops`
- `sza`
- `gtcars`
- `sp500`
- `pizzaplace`
- `exibble`
"
text_2b <- "
There's a quick reference [here](https://commonmark.org/help/).
"
```
Arrange the text snippets as a tibble using the `dplyr::tribble()` function. Then, create a **gt** table and format all columns with `fmt_markdown()`.
```{r}
dplyr::tribble(
~Markdown, ~md,
text_1a, text_2a,
text_1b, text_2b,
) |>
gt() |>
fmt_markdown() |>
tab_options(table.width = px(400))
```
------
### `sub_missing()`: Format missing values
```r
sub_missing(
data,
columns = everything(),
rows = everything(),
missing_text = "---"
)
```
Wherever there is missing data (i.e., `NA` values) a customized mark may present better than the standard `NA` text that would otherwise appear. The `sub_missing()` function allows for this replacement through its `missing_text` argument (where an em dash serves as the default).
##### EXAMPLE
Use `exibble` to create a **gt** table. The `NA` values in different columns will be given replacement text.
```{r}
exibble |>
dplyr::select(-row, -group) |>
gt() |>
sub_missing(
columns = c(num, char),
missing_text = "missing"
) |>
sub_missing(
columns = -c(num, char),
missing_text = "nothing"
)
```
------
### `data_color()`: Set data cell colors using a palette or a color function
```r
data_color(
data,
columns = everything(),
rows = everything(),
direction = c("column", "row"),
target_columns = NULL,
method = c("auto", "numeric", "bin", "quantile", "factor"),
palette = NULL,
domain = NULL,
bins = 8,
quantiles = 4,
levels = NULL,
ordered = FALSE,
na_color = NULL,
alpha = NULL,
reverse = FALSE,
fn = NULL,
apply_to = c("fill", "text"),
autocolor_text = TRUE,
contrast_algo = c("apca", "wcag"),
colors = NULL
)
```
It's possible to add color to data cells according to their values. The `data_color()` function colors all rows of any `columns` supplied. There are two ways to define how cells are colored: (1) through the use of a supplied color palette, and (2) through use of a color mapping function available from the **scales** package.
The first method colorizes cell data according to whether values are character or numeric. The second method provides more control over how cells are colored since we provide an explicit color function and thus other requirements such as bin counts, cut points, or a numeric domain.
##### EXAMPLES
Use `countrypops` to create a **gt** table. Apply a color scale to the `population` column with `method = "numeric"`, four supplied colors in `palette`, and a `domain` range (two-element vector).
```{r}
countrypops |>
dplyr::filter(country_name == "Mongolia") |>
dplyr::select(-contains("code")) |>
tail(10) |>
gt() |>
data_color(
columns = population,
method = "numeric",
palette = c("red", "orange", "green", "blue"),
domain = c(2E6, 4E6)
)
```
Use `pizzaplace` to create a **gt** table. Apply colors from the `red_material` palette (in the `ggsci` pkg but more easily gotten from the `paletteer` package, info at `info_paletteer()`) to the `sold` and `income` columns. Set the `domain` of `scales::col_numeric()` to `NULL` will use the bounds of the available data as the domain.
```{r}
pizzaplace |>
dplyr::filter(type %in% c("chicken", "supreme")) |>
dplyr::group_by(type, size) |>
dplyr::summarize(
sold = dplyr::n(),
income = sum(price),
.groups = "drop"
) |>
gt(rowname_col = "size") |>
data_color(
columns = c(sold, income),
method = "numeric",
palette = "ggsci::red_material"
)
```
------
### `text_transform()`: Perform text transformations with a custom function
```r
text_transform(
data,
fn,
locations = cells_body()
)
```
Text transforming in **gt** is the act of modifying formatted strings in targeted cells. The `text_transform()` function provides the most flexibility of all the `text_*(`) functions in their family of functions. With it, you target the cells to undergo modification in the locations argument while also supplying a function to the `fn` argument.
The function given to `fn` should ideally at the very least take `x` as an input (it stands for the character vector that is essentially the targeted cells) and return a character vector of the same length as the input. Using the construction `function(x) { .. }` for the function is recommended.
##### EXAMPLES
Use a subset of the `sp500` dataset to create a **gt** table. Transform the text in the `date` column using a function supplied to `text_transform()` (via the `fn` argument).
```{r}
sp500 |>
dplyr::slice_head(n = 10) |>
dplyr::select(date, open, close) |>
dplyr::arrange(-dplyr::row_number()) |>
gt() |>
fmt_currency() |>
text_transform(
fn = function(x) {
paste0("Date: ", x)
},
locations = cells_body(columns = date)
)
```
------
### SUMMARY
1. Numbers can be formatted with `fmt_number()`, values in scientific notation with `fmt_scientific()`, percentages with `fmt_percent()`, and currency values with `fmt_currency()`.
2. Options for these number-based formats include: specification of separators, formatting according to a given locale, scaling values, applying a pattern, etc.
3. Dates, times, and date-times can be formatted to numbered styles with `fmt_date()`, `fmt_time()`, and `fmt_datetime()`.
4. Markdown text can be transformed using `fmt_markdown()`.
5. Missing values (i.e., `NA` values) can be substituted with text by using `sub_missing()`.
6. Cells can be colored according to their values with `data_color()`; use functions from the **scales** package and color palettes from the **paletteer** package to help with this.
7. A number of `info_*()` functions provide useful information on supported locales, currencies, date and time styles, and color palettes in **paletteer**.