Skip to content

Instantly share code, notes, and snippets.

@EconomiCurtis
Created April 8, 2020 19:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EconomiCurtis/be8faa57eb90eaba19461c8009bcbb9e to your computer and use it in GitHub Desktop.
Save EconomiCurtis/be8faa57eb90eaba19461c8009bcbb9e to your computer and use it in GitHub Desktop.
"Parts of a gt Table" made with gt
# fun with the gt package.
# replicating the "Parts of a gt Table" at
# https://blog.rstudio.com/2020/04/08/great-looking-tables-gt-0-2/
library(dplyr)
library(gt)
data.frame(
row_label = c("ROW LABEL 1", "ROW LABEL 2"),
group = c("Row Group Label","Row Group Label"),
col1 = c(1.23, 9.87),
col2 = LETTERS[1:2],
col3 = as.Date(c("2020-04-01", "2020-04-02"))
) %>%
gt(
rowname_col = "row_label",
groupname_col = "group"
) %>%
tab_spanner(
label = "Spanner Column Label",
columns = vars(
col1, col2)
) %>%
tab_header(
title = html('<p style="background-color: #e7b3ea; color:black">Title</p>'),
subtitle = html('<p style="background-color: #afb8ef; color:black">Subtitle</p>')
) %>%
cols_label(
col1 = md("Column <br>Label 1"),
col2 = md("Column <br>Label 2"),
col3 = md("Column <br>Label 3"),
) %>%
tab_stubhead(label = html('<p style="background-color: #fbe800">Stubhead Label</p>')) %>%
grand_summary_rows(
columns = vars(col1),
fns = list(`Summary Label` = "sum"),
formatter = fmt_currency,
currency = "USD"
) %>%
grand_summary_rows(
columns = vars(col3),
fns = list(`Summary Label` = "max"),
formatter = fmt_date,
date_style = 1
) %>%
tab_footnote(
footnote = html('<span style="background-color: #d7edc8; padding:4px">Footnote</span>'),
locations = cells_column_labels(columns = vars(col1))
) %>%
tab_source_note(
source_note = html('<span style="background-color: #f7e1a0;">Source Note</span>')
) %>%
tab_style(
style = list(
cell_fill(color = "#bbe6ff")
),
locations = list(
cells_body(columns = vars(col1, col2, col3)),
cells_grand_summary(columns = vars(col1, col2, col3))
)
) %>%
tab_style(
style = list(
cell_fill(color = "#eec400"),
cell_borders(
sides = "all", color = "#eec400", style = "solid", weight = px(5)
)
),
locations = list(
cells_title("title"), cells_title("subtitle")
)
) %>%
tab_style(
style = list(
cell_fill(color = "#aae100"),
cell_borders(
sides = "all", color = "#aae100", style = "solid", weight = px(5)
)),
locations = cells_stubhead()
) %>%
tab_options(
table.border.top.width = "10px",
table.border.top.color = "#eebd00",
table.border.left.width = "10px", #no effect
table.border.left.color = "#eebd00",
table.border.right.width = "100px", #no effect
table.border.right.color = "#eebd00",
heading.background.color = "#eebf00",
heading.border.bottom.color = "#eebd00",
heading.border.bottom.width = "5px",
heading.border.lr.color = "#aae100",
heading.border.lr.width = "50px", #no effect
stub.background.color = "#fbffc4",
column_labels.background.color = "#6cead7",
column_labels.border.top.width = "10px",
column_labels.border.top.color = "#fed7bf",
column_labels.border.bottom.width = "10px",
column_labels.border.bottom.color = "#fed7bf",
column_labels.border.lr.width = "10px", #no effect
column_labels.border.lr.color = "#fed7bf",
column_labels.vlines.width = "110px", #no effect
column_labels.vlines.color = "#fed7bf", #no effect
row_group.background.color = "#fbffc4",
row_group.border.top.color = "#000000",
footnotes.background.color = "#e9e9e9",
footnotes.padding = "5px",
source_notes.background.color = "#e9e9e9",
grand_summary_row.background.color = "#fbffc4",
) %>%
opt_all_caps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment