Created
October 5, 2020 18:40
-
-
Save rich-iannone/b857b19df58d34f886b2e5dedd1d7d64 to your computer and use it in GitHub Desktop.
An example of how to create a gt table and output an RTF file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(gt) | |
library(tidyverse) | |
order_countries <- c("Germany", "Italy", "United States", "Japan") | |
best_gas_mileage_city <- | |
gtcars %>% | |
dplyr::arrange(desc(mpg_c)) %>% | |
dplyr::slice(1) %>% | |
dplyr::mutate(car = paste(mfr, model)) %>% | |
dplyr::pull(car) | |
# Use dplyr functions to get the car with the highest horsepower | |
# this will be used to target the correct cell for a footnote | |
highest_horsepower <- | |
gtcars %>% | |
dplyr::arrange(desc(hp)) %>% | |
dplyr::slice(1) %>% | |
dplyr::mutate(car = paste(mfr, model)) %>% | |
dplyr::pull(car) | |
# Create a display table with `gtcars`, using all of the previous | |
# statements piped together + additional `tab_footnote()` stmts | |
tab <- | |
gtcars %>% | |
dplyr::arrange( | |
factor(ctry_origin, levels = order_countries), | |
mfr, desc(msrp) | |
) %>% | |
dplyr::mutate(car = paste(mfr, model)) %>% | |
dplyr::select(-mfr, -model, -contains("_rpm")) %>% | |
dplyr::group_by(ctry_origin) %>% | |
gt(rowname_col = "car") %>% | |
cols_hide(columns = vars(drivetrain, bdy_style)) %>% | |
cols_move( | |
columns = vars(trsmn), | |
after = vars(trim) | |
) %>% | |
tab_spanner( | |
label = "Performance", | |
columns = vars(mpg_c, mpg_h, hp, trq) | |
) %>% | |
cols_merge( | |
vars(mpg_c, mpg_h), | |
hide_columns = vars(mpg_h), | |
pattern = "{1}c, {2}h" | |
) %>% | |
cols_label( | |
mpg_c = "MPG", | |
hp = "HP", | |
trq = "Torque", | |
year = "Year", | |
trim = "Trim", | |
trsmn = "Transmission", | |
msrp = "MSRP" | |
) %>% | |
fmt_currency( | |
columns = vars(msrp), | |
currency = "USD", | |
decimals = 0 | |
) %>% | |
tab_style( | |
style = cell_text(size = px(12)), | |
locations = cells_body( | |
columns = vars(trim, trsmn, mpg_c, hp, trq) | |
) | |
) %>% | |
text_transform( | |
locations = cells_body(columns = vars(trsmn)), | |
fn = function(x) { | |
speed <- substr(x, 1, 1) | |
type <- | |
dplyr::case_when( | |
substr(x, 2, 3) == "am" ~ "Automatic/Manual", | |
substr(x, 2, 2) == "m" ~ "Manual", | |
substr(x, 2, 2) == "a" ~ "Automatic", | |
substr(x, 2, 3) == "dd" ~ "Direct Drive" | |
) | |
paste(speed, " Speed, ", type) | |
} | |
) %>% | |
tab_header( | |
title = md("The Cars of **gtcars**"), | |
subtitle = "These are some fine automobiles" | |
) %>% | |
tab_source_note( | |
source_note = md( | |
"Source: Various pages within the Edmonds website.") | |
) %>% | |
tab_footnote( | |
footnote = md("Best gas mileage (city) of all the **gtcars**."), | |
locations = cells_body( | |
columns = vars(mpg_c), | |
rows = best_gas_mileage_city) | |
) %>% | |
tab_footnote( | |
footnote = md("The highest horsepower of all the **gtcars**."), | |
locations = cells_body( | |
columns = vars(hp), | |
rows = highest_horsepower) | |
) %>% | |
tab_footnote( | |
footnote = "All prices in U.S. dollars (USD).", | |
locations = cells_column_labels(columns = vars(msrp)) | |
) | |
# Save the table as an RTF file | |
tab %>% gtsave("gtcars.rtf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment