Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as_gtable not respecting cols_width in as_gtable when tab_options(table.width = pct(100))? #1923

Closed
srcm-mark opened this issue Nov 15, 2024 · 3 comments · Fixed by #1931
Closed

Comments

@srcm-mark
Copy link

Prework

Seems potentially related to #1907

Question

I generally using gt in combination with patchwork to output chart/table combos. The development of as_gtable gave me far more flexibility to craft the output (from the former roundtrip to .png. Thanks!!!). I have used table.width = pct(100) to ensure that that table output spans the width of the container, while using the cols_width to apportion individual columns.

Might there be another manner to achieve the objective of forcing the table to fit the output container while maintaining relative spacing across columns?

Seems as_gtable drops the table.width requirement (without as_gtable, table.width and cols_width both retained):

library(gt)
library(gtExtras)
library(tidyverse)

start_date <- "2010-06-07"
end_date <- "2010-06-08"

### no as_gtable
#### table.width not assigned
#### works as expected
sp_tab <- 
  sp500 %>%
  select(-c(volume, adj_close)) %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  gt() %>%
  cols_width(date ~ pct(50))
gt_reprex_image(sp_tab)

#### table.width assigned
#### works as expected
sp_tab <- 
  sp500 %>%
  select(-c(volume, adj_close)) %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  gt() %>%
  tab_options(table.width = pct(100)) %>%
  cols_width(date ~ pct(50))
gt_reprex_image(sp_tab)

### w/ as_gtable
#### table.width not assigned
#### works as expected
sp_tab <- 
  sp500 %>%
  select(-c(volume, adj_close)) %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  gt() %>%
  cols_width(date ~ pct(50)) %>% 
  as_gtable(plot = TRUE)

#### table.width assigned
#### does not work as expected
sp_tab <- 
  sp500 %>%
  select(-c(volume, adj_close)) %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  gt() %>%
  tab_options(table.width = pct(100)) %>%
  cols_width(date ~ pct(50)) %>% 
  as_gtable(plot = TRUE)

Created on 2024-11-14 with reprex v2.1.1

@olivroy
Copy link
Collaborator

olivroy commented Nov 28, 2024

Hi @teunbrand! I wonder if you had time to look into this

@teunbrand
Copy link
Contributor

This wasn't on my radar, but I'll take a look soon!

@teunbrand
Copy link
Contributor

Looking back now at the homebrew table width calculations I wrote, it seems like I didn't anticipate percentage-width columns, just fixed-width columns.
I'd have to study https://www.w3.org/TR/CSS21/tables.html#auto-table-layout and try to mirror that with grid-units somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants