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

Improve as_gtable() width calculations #1931

Merged
merged 4 commits into from
Dec 9, 2024

Conversation

teunbrand
Copy link
Contributor

@teunbrand teunbrand commented Dec 9, 2024

Summary

This PR aims to fix #1923.

I've tried to mirror to CSS specifications more closely with the constraint that grid can't automatically reflow text.
See examples below.

Related GitHub Issues and PRs

Checklist

Examples

Based on the reprex in the linked issue, showing the plot with no total width contraints, a relative width and an absolute width contraint.

devtools::load_all("~/packages/gt/")
#> ℹ Loading gt
library(gtExtras)
library(tidyverse)

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

sp_tab <- 
  sp500 %>%
  select(-c(volume, adj_close)) %>%
  dplyr::filter(date >= start_date & date <= end_date) %>%
  gt() %>%
  cols_width(date ~ pct(50)) %>%
  tab_options(table.font.size = px(12))

# No total width contraints  
plot(sp_tab)

plot(sp_tab %>% tab_options(table.width = pct(80)))

plot(sp_tab %>% tab_options(table.width = px(500)))

Created on 2024-12-09 with reprex v2.1.1

Comment on lines 170 to 177
test <- tbl %>%
cols_width(x ~ pct(20), y ~ px(200)) %>%
as_gtable(tbl, text_grob = dummy_text)

expect_equal(
as.character(test$widths),
c("0.5null", "0.2npc", "150.5625points", "0.5null")
c("0.5null", "100points", "150.5625points", "0.5null")
)
Copy link
Contributor Author

@teunbrand teunbrand Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expectation was introduced in #1776 and had to change, please let me illustrate why.
The HTML has a tiny bit of whitespace to the left of 'x' and plenty to the left of 'y'.

library(gt)

tbl <- data.frame(x = 1, y = 1) |>
  gt() |>
  cols_width(x ~ pct(20), y ~ px(200))

gtExtras::gt_reprex_image(tbl)

Currently, too much space is allocated to the left of 'x'

plot(tbl)

With this PR, we more closely mirror the HTML output:

devtools::load_all("~/packages/gt/")
#> ℹ Loading gt
plot(tbl)

Created on 2024-12-09 with reprex v2.1.1

@rich-iannone rich-iannone self-requested a review December 9, 2024 18:16
Copy link
Member

@rich-iannone rich-iannone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@rich-iannone rich-iannone merged commit cdb22fe into rstudio:master Dec 9, 2024
12 checks passed
@teunbrand teunbrand deleted the relative_gtable_width branch December 9, 2024 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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