Skip to content

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

Description

@srcm-mark

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions