jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
193 stars 26 forks source link

Two column layout with high level header #117

Closed mrcaseb closed 5 months ago

mrcaseb commented 6 months ago

This is a more or less special feature I see people asking for in the nflverse discord. The main idea is to allow a high level tab header in the two column layout. The problem here is formatting. We can either ask the user to set all the html styles themselves or try to help via gt. So in this PR, the function extracts tab header information (including all of the styling) from the header of one of the two tables. This allows the user to do all of the styling through gt.

Example


library(gt)
my_cars <- mtcars %>%
  dplyr::mutate(row_n = dplyr::row_number(), .before = mpg) %>%
  dplyr::select(row_n, mpg:drat)

tab1 <- my_cars %>%
  dplyr::slice(1:16) %>%
  gt() %>%
  gtExtras::gt_color_rows(columns = row_n, domain = 1:32) |> 
  gtExtras::gt_theme_538()

tab2 <- my_cars %>%
  dplyr::slice(17:32) %>%
  gt() %>%
  gtExtras::gt_color_rows(columns = row_n, domain = 1:32) |> 
  gt::tab_header(
    "A LONG AND VERY INFORMATIVE TITLE", "MY SUBTITLE IS ALSO VERY COOL"
  ) |> 
  gtExtras::gt_theme_538() |> 
  gt::tab_options(
    heading.background.color = "gray"
  )

listed_tables <- list(tab1, tab2)

Current implementation

gt_two_column_layout(
  listed_tables, output = "save",
  filename = "basic-two-col.png",
  vwidth = 700, vheight = 620
)

basic-two-col

With Header

gt_two_column_layout(
  listed_tables, output = "save",
  filename = "basic-two-col-title.png",
  vwidth = 600, vheight = 620,
  tab_header_from = "table2"
)

basic-two-col-title

mrcaseb commented 6 months ago

I forgot to add this uses xml2 to extract styling information from raw gt html.

xml2 is already listed in Suggests but I think it's a strong reverse dependency anyway.

mrcaseb commented 6 months ago

There is also room for improvements here. For example high level title and table level subtitles.