Created
November 2, 2022 14:07
-
-
Save MattSandy/9d1bd6f75f2bca07b84adb703018a0d9 to your computer and use it in GitHub Desktop.
GT Table Grouping
This file contains hidden or 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(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