|>
exibble ::select(num, char) |>
dplyrgt() |>
fmt_integer(use_seps = FALSE)
num | char |
---|---|
0 | apricot |
2 | banana |
33 | coconut |
444 | durian |
5550 | NA |
NA | fig |
777000 | grapefruit |
8880000 | honeydew |
fmt_integer()
functionFor this example, we’ll use two columns from the exibble
dataset and create a simple gt table. With fmt_integer()
, we’ll format the num
column as integer values having no digit separators (with the use_seps = FALSE
option).
num | char |
---|---|
0 | apricot |
2 | banana |
33 | coconut |
444 | durian |
5550 | NA |
NA | fig |
777000 | grapefruit |
8880000 | honeydew |
Let’s use a modified version of the countrypops
dataset to create a gt table with row labels. We will format all numeric columns with fmt_integer()
and scale all values by 1 / 1E6
, giving us integer values representing millions of people. We can make clear what the values represent with an informative spanner label via tab_spanner()
.
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_integer(scale_by = 1 / 1E6) |>
tab_spanner(label = "Millions of People", columns = everything())
Millions of People
|
|||||||||
---|---|---|---|---|---|---|---|---|---|
1980 | 1985 | 1990 | 1995 | 2000 | 2005 | 2010 | 2015 | 2020 | |
CHN | 981 | 1,051 | 1,135 | 1,205 | 1,263 | 1,304 | 1,338 | 1,380 | 1,411 |
IND | 697 | 780 | 870 | 964 | 1,060 | 1,155 | 1,241 | 1,323 | 1,396 |
USA | 227 | 238 | 250 | 266 | 282 | 296 | 309 | 321 | 332 |
IDN | 148 | 166 | 182 | 198 | 214 | 229 | 244 | 259 | 272 |
PAK | 81 | 97 | 115 | 133 | 154 | 174 | 194 | 211 | 227 |
Using a subset of the towny
dataset, we can do interesting things with integer values. Through cols_add()
we’ll add the difference
column (which calculates the difference between 2021 and 2001 populations). All numeric values will be formatted with a first pass of fmt_integer()
; a second pass of fmt_integer()
focuses on the difference
column and here we use the force_sign = TRUE
option to draw attention to positive and negative difference values.
towny |>
dplyr::select(name, population_2001, population_2021) |>
dplyr::slice_tail(n = 10) |>
gt() |>
cols_add(difference = population_2021 - population_2001) |>
fmt_integer() |>
fmt_integer(columns = difference, force_sign = TRUE) |>
cols_label_with(fn = function(x) gsub("population_", "", x)) |>
tab_style(
style = cell_fill(color = "gray90"),
locations = cells_body(columns = difference)
)
name | 2001 | 2021 | difference |
---|---|---|---|
Whitchurch-Stouffville | 22,859 | 49,864 | +27,005 |
White River | 993 | 557 | −436 |
Whitestone | 853 | 1,075 | +222 |
Whitewater | 6,520 | 7,225 | +705 |
Wilmot | 14,866 | 21,429 | +6,563 |
Windsor | 208,402 | 229,660 | +21,258 |
Wollaston | 679 | 721 | +42 |
Woodstock | 33,061 | 46,705 | +13,644 |
Woolwich | 18,201 | 26,999 | +8,798 |
Zorra | 8,052 | 8,628 | +576 |