“gt” package


library(gt)
library(tidyverse)
library(glue)



Basic Table (sp500 data)

# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"

# Create a gt table based on preprocessed
# `sp500` table data
sp500 %>%
  filter(date >= start_date & date <= end_date) %>%
  select(-adj_close) %>%
  gt() %>%
  tab_header(
    title = "S&P 500",
    subtitle = glue("{start_date} to {end_date}")) %>%
  fmt_date(
    columns = vars(date),
    date_style = 3) %>%
  fmt_currency(
    columns = vars(open, high, low, close),
    currency = "USD") %>%
  fmt_number(
    columns = vars(volume),
    suffixing = TRUE)
S&P 500
2010-06-07 to 2010-06-14
date open high low close volume
Mon, Jun 14, 2010 $1,095.00 $1,105.91 $1,089.03 $1,089.63 4.43B
Fri, Jun 11, 2010 $1,082.65 $1,092.25 $1,077.12 $1,091.60 4.06B
Thu, Jun 10, 2010 $1,058.77 $1,087.85 $1,058.77 $1,086.84 5.14B
Wed, Jun 9, 2010 $1,062.75 $1,077.74 $1,052.25 $1,055.69 5.98B
Tue, Jun 8, 2010 $1,050.81 $1,063.15 $1,042.17 $1,062.00 6.19B
Mon, Jun 7, 2010 $1,065.84 $1,071.36 $1,049.86 $1,050.47 5.47B



Step by step create table (exibble dataset)

1. exibble data:

exibble
## # A tibble: 8 x 9
##           num char      fctr  date     time  datetime       currency row   group
##         <dbl> <chr>     <fct> <chr>    <chr> <chr>             <dbl> <chr> <chr>
## 1       0.111 apricot   one   2015-01~ 13:35 2018-01-01 02~    50.0  row_1 grp_a
## 2       2.22  banana    two   2015-02~ 14:40 2018-02-02 14~    18.0  row_2 grp_a
## 3      33.3   coconut   three 2015-03~ 15:45 2018-03-03 03~     1.39 row_3 grp_a
## 4     444.    durian    four  2015-04~ 16:50 2018-04-04 15~ 65100    row_4 grp_a
## 5    5550     <NA>      five  2015-05~ 17:55 2018-05-05 04~  1326.   row_5 grp_b
## 6      NA     fig       six   2015-06~ <NA>  2018-06-06 16~    13.3  row_6 grp_b
## 7  777000     grapefru~ seven <NA>     19:10 2018-07-07 05~    NA    row_7 grp_b
## 8 8880000     honeydew  eight 2015-08~ 20:20 <NA>               0.44 row_8 grp_b



2. Create simple table by gt package

exibble %>% 
  gt()

num char fctr date time datetime currency row group
1.111e-01 apricot one 2015-01-15 13:35 2018-01-01 02:22 49.950 row_1 grp_a
2.222e+00 banana two 2015-02-15 14:40 2018-02-02 14:33 17.950 row_2 grp_a
3.333e+01 coconut three 2015-03-15 15:45 2018-03-03 03:44 1.390 row_3 grp_a
4.444e+02 durian four 2015-04-15 16:50 2018-04-04 15:55 65100.000 row_4 grp_a
5.550e+03 NA five 2015-05-15 17:55 2018-05-05 04:00 1325.810 row_5 grp_b
NA fig six 2015-06-15 NA 2018-06-06 16:11 13.255 row_6 grp_b
7.770e+05 grapefruit seven NA 19:10 2018-07-07 05:22 NA row_7 grp_b
8.880e+06 honeydew eight 2015-08-15 20:20 NA 0.440 row_8 grp_b


3. Formatting data in columns

exibble %>%
  gt() %>%
  fmt_number(columns = vars(num), decimals = 2) %>%
  fmt_date(columns = vars(date), date_style = 6) %>%
  fmt_time(columns = vars(time), time_style = 4) %>%
  fmt_datetime(columns = vars(datetime), date_style = 6, time_style = 4) %>%
  fmt_currency(columns = vars(currency), currency = "EUR")

num char fctr date time datetime currency row group
0.11 apricot one Jan 15, 2015 1:35 PM Jan 1, 2018 2:22 AM €49.95 row_1 grp_a
2.22 banana two Feb 15, 2015 2:40 PM Feb 2, 2018 2:33 PM €17.95 row_2 grp_a
33.33 coconut three Mar 15, 2015 3:45 PM Mar 3, 2018 3:44 AM €1.39 row_3 grp_a
444.40 durian four Apr 15, 2015 4:50 PM Apr 4, 2018 3:55 PM €65,100.00 row_4 grp_a
5,550.00 NA five May 15, 2015 5:55 PM May 5, 2018 4:00 AM €1,325.81 row_5 grp_b
NA fig six Jun 15, 2015 NA Jun 6, 2018 4:11 PM €13.26 row_6 grp_b
777,000.00 grapefruit seven NA 7:10 PM Jul 7, 2018 5:22 AM NA row_7 grp_b
8,880,000.00 honeydew eight Aug 15, 2015 8:20 PM NA €0.44 row_8 grp_b


Many formatter functions:

  • have num display numbers with exactly 2 decimal places using
    fmt_number()
  • show nicely formatted dates in date using date_style 6 (the m_day_year style) with
    fmt_date()
  • format the 24-h time values in time to time_style 4 (the hm_p style) with
    fmt_time()
  • make the datetimes in datetime formatted as such with the
    fmt_datetime()
  • transform the currency column with
    fmt_currency() to show us values in the euro currency (currency = "EUR")



4. Table with a header and source note

add header

tab_header(
title = md("This is theexibbledataset in **gt**"),
subtitle = "It is one of six datasets in the package"

add source note
tab_source_note(md("More information is available at?exibble`.“))’

exibble %>%
  gt() %>%
   tab_header(
    title = md("This is the `exibble` dataset in **gt**"),
    subtitle = "It is one of six datasets in the package"
  ) %>%
  fmt_number(columns = vars(num), decimals = 2) %>%
  fmt_date(columns = vars(date), date_style = 6) %>%
  fmt_time(columns = vars(time), time_style = 4) %>%
  fmt_datetime(columns = vars(datetime), date_style = 6, time_style = 4) %>%
  fmt_currency(columns = vars(currency), currency = "EUR")%>%
  tab_source_note(md("More information is available at `?exibble`."))
This is the exibble dataset in gt
It is one of six datasets in the package
num char fctr date time datetime currency row group
0.11 apricot one Jan 15, 2015 1:35 PM Jan 1, 2018 2:22 AM €49.95 row_1 grp_a
2.22 banana two Feb 15, 2015 2:40 PM Feb 2, 2018 2:33 PM €17.95 row_2 grp_a
33.33 coconut three Mar 15, 2015 3:45 PM Mar 3, 2018 3:44 AM €1.39 row_3 grp_a
444.40 durian four Apr 15, 2015 4:50 PM Apr 4, 2018 3:55 PM €65,100.00 row_4 grp_a
5,550.00 NA five May 15, 2015 5:55 PM May 5, 2018 4:00 AM €1,325.81 row_5 grp_b
NA fig six Jun 15, 2015 NA Jun 6, 2018 4:11 PM €13.26 row_6 grp_b
777,000.00 grapefruit seven NA 7:10 PM Jul 7, 2018 5:22 AM NA row_7 grp_b
8,880,000.00 honeydew eight Aug 15, 2015 8:20 PM NA €0.44 row_8 grp_b
More information is available at ?exibble.



5. organizing rows into row groups

Add gt(rowname_col = "row",groupname_col = "group")

exibble %>%
  gt(rowname_col = "row",groupname_col = "group") %>%
   tab_header(
    title = md("This is the `exibble` dataset in **gt**"),
    subtitle = "It is one of six datasets in the package"
  ) %>%
  fmt_number(columns = vars(num), decimals = 2) %>%
  fmt_date(columns = vars(date), date_style = 6) %>%
  fmt_time(columns = vars(time), time_style = 4) %>%
  fmt_datetime(columns = vars(datetime), date_style = 6, time_style = 4) %>%
  fmt_currency(columns = vars(currency), currency = "EUR")%>%
  tab_source_note(md("More information is available at `?exibble`."))
This is the exibble dataset in gt
It is one of six datasets in the package
num char fctr date time datetime currency
grp_a
row_1 0.11 apricot one Jan 15, 2015 1:35 PM Jan 1, 2018 2:22 AM €49.95
row_2 2.22 banana two Feb 15, 2015 2:40 PM Feb 2, 2018 2:33 PM €17.95
row_3 33.33 coconut three Mar 15, 2015 3:45 PM Mar 3, 2018 3:44 AM €1.39
row_4 444.40 durian four Apr 15, 2015 4:50 PM Apr 4, 2018 3:55 PM €65,100.00
grp_b
row_5 5,550.00 NA five May 15, 2015 5:55 PM May 5, 2018 4:00 AM €1,325.81
row_6 NA fig six Jun 15, 2015 NA Jun 6, 2018 4:11 PM €13.26
row_7 777,000.00 grapefruit seven NA 7:10 PM Jul 7, 2018 5:22 AM NA
row_8 8,880,000.00 honeydew eight Aug 15, 2015 8:20 PM NA €0.44
More information is available at ?exibble.



6. Using spanner column labels

add tab_spanner(label = "Dates and Times", columns = matches("date|time"))

exibble %>%
  gt(rowname_col = "row", groupname_col = "group") %>%
  tab_spanner(label = "Dates and Times", columns = matches("date|time")) %>%
   tab_header(
    title = md("This is the `exibble` dataset in **gt**"),
    subtitle = "It is one of six datasets in the package"
  ) %>%
  fmt_number(columns = vars(num), decimals = 2) %>%
  fmt_date(columns = vars(date), date_style = 6) %>%
  fmt_time(columns = vars(time), time_style = 4) %>%
  fmt_datetime(columns = vars(datetime), date_style = 6, time_style = 4) %>%
  fmt_currency(columns = vars(currency), currency = "EUR")%>%
  tab_source_note(md("More information is available at `?exibble`."))
This is the exibble dataset in gt
It is one of six datasets in the package
num char fctr Dates and Times currency
date time datetime
grp_a
row_1 0.11 apricot one Jan 15, 2015 1:35 PM Jan 1, 2018 2:22 AM €49.95
row_2 2.22 banana two Feb 15, 2015 2:40 PM Feb 2, 2018 2:33 PM €17.95
row_3 33.33 coconut three Mar 15, 2015 3:45 PM Mar 3, 2018 3:44 AM €1.39
row_4 444.40 durian four Apr 15, 2015 4:50 PM Apr 4, 2018 3:55 PM €65,100.00
grp_b
row_5 5,550.00 NA five May 15, 2015 5:55 PM May 5, 2018 4:00 AM €1,325.81
row_6 NA fig six Jun 15, 2015 NA Jun 6, 2018 4:11 PM €13.26
row_7 777,000.00 grapefruit seven NA 7:10 PM Jul 7, 2018 5:22 AM NA
row_8 8,880,000.00 honeydew eight Aug 15, 2015 8:20 PM NA €0.44
More information is available at ?exibble.



7. Mark the cells:

add color and font change


tab_style(
style = list(
cell_fill(color = "lightcyan"),
cell_text(color = "white"),
cell_text(weight = "bold"),
cell_text(style = "italic"),
locations = list(
cells_body(columns=c(1), rows=c(1)),
cells_body(columns=c(2), rows=c(2)),
cells_body(columns=c(3), rows=c(3)),
cells_body(columns=c(4), rows=c(4)),
cells_body(columns=c(5), rows=c(5)),
cells_body(columns=c(6), rows=c(6))
))


add footnote
tab_footnote(
footnote = md("The **smallest** by number"),
locations = list(
cells_body(columns=c(1), rows=c(1))))

exibble %>%
  gt(rowname_col = "row", groupname_col = "group") %>%
  tab_spanner(label = "Dates and Times", columns = matches("date|time")) %>%
   tab_header(
    title = md("This is the `exibble` dataset in **gt**"),
    subtitle = "It is one of six datasets in the package"
  ) %>%
  fmt_number(columns = vars(num), decimals = 2) %>%
  fmt_date(columns = vars(date), date_style = 6) %>%
  fmt_time(columns = vars(time), time_style = 4) %>%
  fmt_datetime(columns = vars(datetime), date_style = 6, time_style = 4) %>%
  fmt_currency(columns = vars(currency), currency = "EUR")%>%
  tab_style(
    style = list(
      cell_fill(color = "blue"),
      cell_text(color = "white"),
      cell_text(weight = "bold"),
      cell_text(style = "italic")
      ),
    locations = list(
        cells_body(columns=c(1), rows=c(5)),
        cells_body(columns=c(1), rows=c(7)),
        cells_body(columns=c(1), rows=c(8))
    ))%>%
  tab_source_note(md("More information is available at `?exibble`.")) %>%
  tab_footnote(
    footnote = md("The **smallest** by number"),
    locations = list(
        cells_body(columns=c(1), rows=c(1))))
This is the exibble dataset in gt
It is one of six datasets in the package
num char fctr Dates and Times currency
date time datetime
grp_a
row_1 0.111 apricot one Jan 15, 2015 1:35 PM Jan 1, 2018 2:22 AM €49.95
row_2 2.22 banana two Feb 15, 2015 2:40 PM Feb 2, 2018 2:33 PM €17.95
row_3 33.33 coconut three Mar 15, 2015 3:45 PM Mar 3, 2018 3:44 AM €1.39
row_4 444.40 durian four Apr 15, 2015 4:50 PM Apr 4, 2018 3:55 PM €65,100.00
grp_b
row_5 5,550.00 NA five May 15, 2015 5:55 PM May 5, 2018 4:00 AM €1,325.81
row_6 NA fig six Jun 15, 2015 NA Jun 6, 2018 4:11 PM €13.26
row_7 777,000.00 grapefruit seven NA 7:10 PM Jul 7, 2018 5:22 AM NA
row_8 8,880,000.00 honeydew eight Aug 15, 2015 8:20 PM NA €0.44
More information is available at ?exibble.

1 The smallest by number



Reference

  1. gt 0.2.0.9000 https://gt.rstudio.com/
  2. Great Looking Tables: gt (v0.2) https://www.r-bloggers.com/great-looking-tables-gt-v0-2/amp/
  3. Playing with gt R Packagehttps://medium.com/the-artificial-impostor/playing-with-rstudio-gt-r-package-2f37a340c23f