Traditionally I have been an ardent user of kable + kableExtra when it comes to creating tables. These packages have served me well, however the CRAN release of a new player in the table package space - gt - promted me to try it out and explore some of the features it had to offer.
Published
May 2, 2020
gt(Iannone, Cheng, and Schloerke 2020) has been under development for a while, but a stable version has recently been released to CRAN. In order to try the package out we first need some data for creating tables. I’ll be using some Bitcoin price data (shameless self plug for my bitmexr(Fisher 2020) package that has just landed on CRAN - check it out here).
For demonstration purposes, I pulled the OHLC prices for the last 10 days.
The most basic table without any formatting applied looks like this:
bitcoin %>%gt()
timestamp
open
high
low
close
trades
volume
2020-04-23
6848.5
7189.0
6822.0
7133.5
572398
1450803027
2020-04-24
7133.5
7802.0
7023.5
7492.0
935125
2488032747
2020-04-25
7492.0
7610.0
7380.0
7503.5
621057
1473830307
2020-04-26
7503.5
7745.0
7431.0
7542.0
641738
1362595381
2020-04-27
7542.0
7720.0
7450.0
7702.5
674889
1432590477
2020-04-28
7702.5
7836.5
7625.0
7788.0
811266
1663495897
2020-04-29
7788.0
7795.5
7651.0
7752.0
520240
1154456663
2020-04-30
7752.0
8978.0
7706.0
8794.0
2384499
5110631297
2020-05-01
8794.0
9481.0
8407.0
8627.5
3661752
5794365072
2020-05-02
8627.5
9065.0
8622.5
8832.0
2085264
2573647732
To get started, I borrowed some ideas straight from an example on https://gt.rstudio.com/ and applied some simple formatting to the raw data in the table.
Added a header containing the title and subtitle of the table
Format the price columns with the appropriate currency symbol
Format the date column
Format the trades and volume column with appropriate suffixes
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
tab
Bitcoin price
23/04/2020 to 02/05/2020
timestamp
open
high
low
close
trades
volume
23 April
$6,848.50
$7,189.00
$6,822.00
$7,133.50
572.40K
1.45B
24 April
$7,133.50
$7,802.00
$7,023.50
$7,492.00
935.12K
2.49B
25 April
$7,492.00
$7,610.00
$7,380.00
$7,503.50
621.06K
1.47B
26 April
$7,503.50
$7,745.00
$7,431.00
$7,542.00
641.74K
1.36B
27 April
$7,542.00
$7,720.00
$7,450.00
$7,702.50
674.89K
1.43B
28 April
$7,702.50
$7,836.50
$7,625.00
$7,788.00
811.27K
1.66B
29 April
$7,788.00
$7,795.50
$7,651.00
$7,752.00
520.24K
1.15B
30 April
$7,752.00
$8,978.00
$7,706.00
$8,794.00
2.38M
5.11B
1 May
$8,794.00
$9,481.00
$8,407.00
$8,627.50
3.66M
5.79B
2 May
$8,627.50
$9,065.00
$8,622.50
$8,832.00
2.09M
2.57B
Starting to look a little more presentable. Next, I
Added some colour to highlight changes in the trades and volume columns
Applied some formatting to the column names.
I also changed the number of decimal places to 0 (2 decimal places seemd overkill for this example).
A cool feature of gt is that you can rename the column labels to something different, but refer to the original labels for any further manipulation.
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
Warning: `columns = vars(...)` has been deprecated in gt 0.3.0:
* please use `columns = c(...)` instead
tab
Bitcoin price
23/04/2020 to 02/05/2020
Date
Price
Trades
Volume
Open
High
Low
Close
23 April
$6,848
$7,189
$6,822
$7,134
572.40K
1.45B
24 April
$7,134
$7,802
$7,024
$7,492
935.12K
2.49B
25 April
$7,492
$7,610
$7,380
$7,504
621.06K
1.47B
26 April
$7,504
$7,745
$7,431
$7,542
641.74K
1.36B
27 April
$7,542
$7,720
$7,450
$7,702
674.89K
1.43B
28 April
$7,702
$7,836
$7,625
$7,788
811.27K
1.66B
29 April
$7,788
$7,796
$7,651
$7,752
520.24K
1.15B
30 April
$7,752
$8,978
$7,706
$8,794
2.38M
5.11B
1 May
$8,794
$9,481
$8,407
$8,628
3.66M
5.79B
2 May
$8,628
$9,065
$8,622
$8,832
2.09M
2.57B
Finally, it is also very easy to add custom html to the tables using the text_transformations() function (again inspired by https://gt.rstudio.com/). The rocket and sad face indicate whether the closing price was greater or less than the opening price for each dat
rocket <-"<span style=\"color:green\">🚀</span>"sadface <-"<span style=\"color:red\">😢</span>"tab <- tab %>%text_transform(locations =cells_body(columns ="close",rows = close > open ),fn =function(x) paste(x, rocket) ) %>%text_transform(locations =cells_body(columns ="close",rows = close < open ),fn =function(x) paste(x, sadface) )tab
Bitcoin price
23/04/2020 to 02/05/2020
Date
Price
Trades
Volume
Open
High
Low
Close
23 April
$6,848
$7,189
$6,822
$7,134 🚀
572.40K
1.45B
24 April
$7,134
$7,802
$7,024
$7,492 🚀
935.12K
2.49B
25 April
$7,492
$7,610
$7,380
$7,504 🚀
621.06K
1.47B
26 April
$7,504
$7,745
$7,431
$7,542 🚀
641.74K
1.36B
27 April
$7,542
$7,720
$7,450
$7,702 🚀
674.89K
1.43B
28 April
$7,702
$7,836
$7,625
$7,788 🚀
811.27K
1.66B
29 April
$7,788
$7,796
$7,651
$7,752 😢
520.24K
1.15B
30 April
$7,752
$8,978
$7,706
$8,794 🚀
2.38M
5.11B
1 May
$8,794
$9,481
$8,407
$8,628 😢
3.66M
5.79B
2 May
$8,628
$9,065
$8,622
$8,832 🚀
2.09M
2.57B
Looks good! I’ve barely scratched the surface of what gt can do here and the package website is well worth a look if you are interested.
Bonus table
One experimental feature in gt is the ability to add a ggplot (or any image for that matter) to a table. Yes thats right, a ggplot in a table. I did have to do a bit of rummaging in the package repo to get this to work, but the idea is really cool.
To demonstrate this functionality I pulled some new Bitcoin price data - the hourly OHLC for the whole on April 2020. The goal is to summarise this information for weekly intervals, with the total volume and trades and a plot of the price action for that week.
First I define a few functions that will help in creating the table/plot combo.
The first is just a simple helper function which generates some summary information for the final table
The second is the ggplot object that will be used to visualise each weeks data.
The third was borrowed straight from gt’s repo here. I think some of this functionality is still under development, so is not yet available in the CRAN release.
2022 update: It is now much easier to support ggplot objects in gt tables! See here
We’re now ready to prepare the data for the table.