Creating pretty tables with gt()!
- This was my first time using gt() and I thought it was really easy to use and the function names are so intuitive (e.g. if you want to merge to columns together… the function is cols_merg())
- I’ll be using the exibble dataset (with a few modifications)
- Exibble is an example tibble that is part of the gt pacakage
library(tidyverse)
library(gt)
my_table <- exibble %>%
mutate(perc = c(10, 20, 30, 40, 50, 60, 70, 80),
sd = c(.1, .1, .25, .2, .1, .1, .3, .25)) %>%
arrange(desc(currency)) %>%
drop_na(currency)
head(my_table)## # A tibble: 6 x 11
## num char fctr date time datetime currency row group perc sd
## <dbl> <chr> <fct> <chr> <chr> <chr> <dbl> <chr> <chr> <dbl> <dbl>
## 1 444. durian four 2015-~ 16:50 2018-04-0~ 65100 row_4 grp_a 40 0.2
## 2 5550 NA five 2015-~ 17:55 2018-05-0~ 1326. row_5 grp_b 50 0.1
## 3 0.111 apric~ one 2015-~ 13:35 2018-01-0~ 50.0 row_1 grp_a 10 0.1
## 4 2.22 banana two 2015-~ 14:40 2018-02-0~ 18.0 row_2 grp_a 20 0.1
## 5 NA fig six 2015-~ NA 2018-06-0~ 13.3 row_6 grp_b 60 0.1
## 6 33.3 cocon~ three 2015-~ 15:45 2018-03-0~ 1.39 row_3 grp_a 30 0.25
my_table$currency[1] <- 55.20
my_table$currency[2] <- 42.99
my_table %>%
gt(
rowname_col = "row",
groupname_col = "group"
) %>%
cols_hide( # hide columns
columns = vars(date, time, fctr, char, datetime) # make sure to use vars()
) %>%
fmt_number( # format numbers to 2 decimal places
columns = vars(num),
decimals = 2
) %>%
fmt_number( ### get parentheses!
columns = vars(sd),
pattern = "({x})"
) %>%
fmt_percent( # format percent
columns = vars(perc),
scale_values = FALSE, # by default this is set to try which multiplies the variable by 100
decimals = 0
) %>%
cols_merge( # merge columns into 1
columns = vars(perc, sd) #,
# pattern = "{1} ({2})" # or this also works to add parentheses in columns you are merging
) %>%
cols_label( # added this
perc = "% (sd)",
) %>%
fmt_currency( # add currency
columns = vars(currency),
currency = "AUD"
) %>%
tab_header( # title and subtitle
title = md("Example Table from **exibble**"), # md is to allow markdown formatting
subtitle = md("`exibble` is an gt dataset")
) %>%
tab_footnote( # you can add 1 or more footnotes
footnote = "These are prices < 15",
locations = cells_body(
columns = vars(currency),
rows = currency < 15
)
) %>%
tab_footnote(
footnote = "These are numbers > 500",
locations = cells_body(
columns = vars(num),
rows = num > 500
)
) %>%
data_color(
columns = vars(currency),
colors = scales::col_numeric(
palette = c("lightblue", "darkblue"),
domain = c(0, 60))
)| Example Table from exibble | |||
|---|---|---|---|
exibble is an gt dataset |
|||
| num | currency | % (sd) | |
| grp_a | |||
| row_4 | 444.40 | $55.20 | 40% (0.20) |
| row_1 | 0.11 | $49.95 | 10% (0.10) |
| row_2 | 2.22 | $17.95 | 20% (0.10) |
| row_3 | 33.33 | $1.391 | 30% (0.25) |
| grp_b | |||
| row_5 | 5,550.002 | $42.99 | 50% (0.10) |
| row_6 | NA | $13.261 | 60% (0.10) |
| row_8 | 8,880,000.002 | $0.441 | 80% (0.25) |
|
1
These are prices < 15
2
These are numbers > 500
|
|||