Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Created November 2, 2022 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattSandy/9d1bd6f75f2bca07b84adb703018a0d9 to your computer and use it in GitHub Desktop.
Save MattSandy/9d1bd6f75f2bca07b84adb703018a0d9 to your computer and use it in GitHub Desktop.
GT Table Grouping
library(tidyverse)
library(gt)
mtcars %>%
group_by(gear,cyl) %>%
summarise(
n = n(),
mpg = mean(mpg)
) %>% (function(df){
df$gear[which(duplicated(df$gear))] <- ' '
return(df)
}) %>%
data.frame() %>%
gt() %>%
tab_header(
title = "Gears and Cyclinders Effect on MPG",
subtitle = glue("Groupings Show Counts and Mean MPG")
) %>%
tab_style(
style = list(
cell_fill(color = "lightcyan"),
cell_text(weight = "bold"),
cell_borders(sides = "top", color = "#000000", style = "solid", weight = px(1))
),
locations = cells_body(
columns = 1:4,
rows = ((gear %>% as.numeric()) > 0)
)
) %>%
tab_style(
style = list(
cell_fill(color = "#f6f6f6"),
cell_text(weight = "bold")
),
locations = cells_body(
columns = 2:4,
rows = n>0
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment