The fmt_spelled_num()
function
Let’s use a summarized version of the gtcars
dataset to create a gt table. fmt_spelled_num()
is used to transform integer values into spelled-out numbering (in the n
column). That formatted column of numbers-as-words is given cell background colors via data_color()
(the underlying numerical values are always available).
gtcars |>
dplyr:: count (mfr, ctry_origin) |>
dplyr:: arrange (ctry_origin) |>
gt (rowname_col = "mfr" , groupname_col = "ctry_origin" ) |>
cols_label (n = "No. of Entries" ) |>
fmt_spelled_num () |>
tab_stub_indent (rows = everything (), indent = 2 ) |>
data_color (
columns = n,
method = "numeric" ,
palette = "viridis" ,
alpha = 0.8
) |>
opt_all_caps () |>
opt_vertical_padding (scale = 0.5 ) |>
cols_align (align = "center" , columns = n)
No. of Entries
Germany
Audi
five
BMW
five
Mercedes-Benz
two
Porsche
four
Italy
Ferrari
nine
Lamborghini
three
Maserati
three
Japan
Acura
one
Nissan
one
United Kingdom
Aston Martin
four
Bentley
one
Jaguar
one
Lotus
one
McLaren
one
Rolls-Royce
two
United States
Chevrolet
one
Dodge
one
Ford
one
Tesla
one
With a considerable amount of dplyr and tidyr work done to the pizzaplace
dataset, we can create a new gt table. fmt_spelled_num()
will be used here to transform the integer values in the rank
column. We’ll do so with a special pattern
that puts the word ‘Number’ in front of every spelled-out number.
pizzaplace |>
dplyr:: mutate (month = lubridate:: month (date, label = TRUE )) |>
dplyr:: filter (month %in% month.abb[1 : 6 ]) |>
dplyr:: group_by (name, month) |>
dplyr:: summarize (sum = sum (price), .groups = "drop" ) |>
dplyr:: arrange (month, desc (sum)) |>
dplyr:: group_by (month) |>
dplyr:: slice_head (n = 5 ) |>
dplyr:: mutate (rank = dplyr:: row_number ()) |>
dplyr:: ungroup () |>
dplyr:: select (- sum) |>
tidyr:: pivot_wider (names_from = month, values_from = c (name)) |>
gt () |>
fmt_spelled_num (columns = rank, pattern = "Number {x}" ) |>
opt_all_caps () |>
cols_align (columns = - rank, align = "center" ) |>
cols_width (
rank ~ px (120 ),
everything () ~ px (100 )
) |>
opt_table_font (stack = "rounded-sans" ) |>
tab_options (table.font.size = px (14 ))
rank
Jan
Feb
Mar
Apr
May
Jun
Number one
bbq_ckn
cali_ckn
bbq_ckn
bbq_ckn
bbq_ckn
cali_ckn
Number two
thai_ckn
bbq_ckn
thai_ckn
thai_ckn
cali_ckn
thai_ckn
Number three
cali_ckn
thai_ckn
cali_ckn
classic_dlx
thai_ckn
bbq_ckn
Number four
pepperoni
southw_ckn
spicy_ital
southw_ckn
classic_dlx
classic_dlx
Number five
sicilian
four_cheese
classic_dlx
cali_ckn
spicy_ital
spicy_ital
Let’s make a table that compares how the numbers from 1
to 10
are spelled across a small selection of languages. Here we use fmt_spelled_num()
with each column, ensuring that the locale
value matches that of the column name.
dplyr:: tibble (
num = 1 : 10 ,
en = num,
fr = num,
de = num,
es = num,
pl = num,
bg = num,
ko = num,
zh = num
) |>
gt (rowname_col = "num" ) |>
fmt_spelled_num (columns = en, locale = "en" ) |>
fmt_spelled_num (columns = fr, locale = "fr" ) |>
fmt_spelled_num (columns = de, locale = "de" ) |>
fmt_spelled_num (columns = es, locale = "es" ) |>
fmt_spelled_num (columns = pl, locale = "pl" ) |>
fmt_spelled_num (columns = bg, locale = "bg" ) |>
fmt_spelled_num (columns = ko, locale = "ko" ) |>
fmt_spelled_num (columns = zh, locale = "zh" ) |>
cols_label_with (fn = function (x) md (paste0 ("`" , x, "`" ))) |>
tab_spanner (
label = "Numbers in the specified locale" ,
columns = everything ()
) |>
cols_align (align = "left" , columns = everything ()) |>
cols_width (
c (en, fr, de, es, pl, bg) ~ px (100 ),
c (ko, zh) ~ px (50 )
) |>
opt_horizontal_padding (scale = 2 ) |>
opt_vertical_padding (scale = 0.5 )
Numbers in the specified locale
en
fr
de
es
pl
bg
ko
zh
1
one
un
eins
uno
jedno
едно
일
一
2
two
deux
zwei
dos
dwa
две
이
二
3
three
trois
drei
tres
trzy
три
삼
三
4
four
quatre
vier
cuatro
cztery
четири
사
四
5
five
cinq
fünf
cinco
pięć
пет
오
五
6
six
six
sechs
seis
sześć
шест
육
六
7
seven
sept
sieben
siete
siedem
седем
칠
七
8
eight
huit
acht
ocho
osiem
осем
팔
八
9
nine
neuf
neun
nueve
dziewięć
девет
구
九
10
ten
dix
zehn
diez
dziesięć
десет
십
十