Skip to content

Pct column widths #1776

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

Merged
merged 4 commits into from
Jul 12, 2024
Merged

Pct column widths #1776

merged 4 commits into from
Jul 12, 2024

Conversation

teunbrand
Copy link
Contributor

Summary

This PR aims to fix #1771.
Briefly, it works by accounting for pct()-widths in grid_layout_widths().
The reprex from the issue now renders:

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

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

sp_tab <- 
  sp500 |>
  dplyr::filter(date >= start_date & date <= end_date) |>
  gt() |>
  cols_width(date ~ pct(50)) |>
  as_gtable(plot = TRUE)

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

Related GitHub Issues and PRs

Checklist

@olivroy
Copy link
Collaborator

olivroy commented Jul 12, 2024

Awesome! I have a small request. Do you know how grid output could convert
to be a linebreak \n (\n is recognized by grid). I tried searching for the right place to implement, but couldn't figure it out.

This reprex:

data.frame(estimate = pi, std.error = pi / 10) |> 
  gt::gt() |> 
  gt::fmt_number() |> 
  gt::cols_merge(columns = c(estimate, std.error), pattern = "{1}  <br> ({2})")

renders to this in html
image

It'd be great if it rendered to the same thing in gtable, but currently, it just shows
.

A well-placed

gsub("<br>", "\n", x)

would do the trick, but I couldn't figure out where to put this. (for #1767 )

@teunbrand
Copy link
Contributor Author

It isn't really related to this PR, but this is exactly the kind of thing why the as_gtable(text_grob) argument exists.
If you don't want to render text verbatim, you can use:

data.frame(estimate = pi, std.error = pi / 10) |> 
  gt::gt() |> 
  gt::fmt_number() |> 
  gt::cols_merge(columns = c(estimate, std.error), pattern = "{1} \n ({2})") |>
  gt::as_gtable(
    plot = TRUE, text_grob = function(label, ...)  {
      grid::textGrob(gsub("<br>", "\n", label), ...)
    }
  )

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

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.

Thank you!

@rich-iannone rich-iannone merged commit b3ff1d8 into rstudio:master Jul 12, 2024
12 checks passed
@teunbrand teunbrand deleted the pct_column_widths branch July 12, 2024 14:49
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.

cols_width specifications and as_gtable
3 participants