-
Notifications
You must be signed in to change notification settings - Fork 213
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
Render tables as gtable #1563
Render tables as gtable #1563
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks exciting! I will test this, here are some minor suggestions for tests.
@teunbrand , this is excellent! Thank you so much for taking this on. |
Co-authored-by: olivroy <[email protected]>
Because we're still testing older R versions, we're stuck with using |
Oops, sorry my bad 😅 |
@teunbrand do you feel this is ready for a final review (i.e., no more work on your end for the time being)? |
I think this is almost complete, the only open question is about how this should face the user. |
Somewhat strangely, tests on R 3.6.3 aren't passing because of something new. Looks like a problem both on the gt side and possibly with gtable (and maybe stemming from the gt column widths problem?). I will investigate this in a short while. |
Part of the issue might be the old v1 units {grid} had before R 4.0.0, back when |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Summary
This PR aims to fix #180 and fix #961, and conflicts with #1015.
Briefly, it adds a
render_as_gtable()
function that builds the table as a gtable.Related GitHub Issues and PRs
Checklist
testthat
unit tests totests/testthat
for any new functionality.Description
I was asked by @thomasp85 whether I could take a stab at #180, so that patchwork and other (gg)plot composition packages could integrate nice tables that {gt} offers. This PR adds a method for rendering tables in the grid graphics system. It consists of two important parts.
Table layout
For {gtable}, we need to know the layout of the table in advance. For every cell in the table, we need to know the left/right/top/bottom cell in the gtable it occupies, similar to the rowspan and colspan attributes determine in html how wide or tall a cell is. I'm not proficient enough in html to parse the table layout from the html that the
render_as_html()
function produces. Instead, the layout is computed separately from, but analogous to, the html code. All the important layout code is captured in thecreate_{part}_component_g()
functions ('g' for grid or gtable), that mirror thecreate_{part}_component_h()
('h' for html).Rendering cells
For rendering cells, I've opted for following the css classes and styles that are used in the html output. These are parsed and translated to graphical parameters that {grid} understands. Mostly, this requires a bunch of parsers and a way to consolidate different classes/styles, but it works well enough for most cases. Of course, not all html/css features can be translated to grid, like
overflow-x
, collapsing borders, automatic text wrapping etc. These are thus not implemented. In addition, if the cell text itself contains html, this is not natively understood by grid. To make this slightly less of a pain,render_as_gtable()
has atext_grob
argument that can take e.g.gridtext::richtext_grob()
to render the html in the cells better than vanilla grid would.Current state
I think I'm finished with all the internal cogs and gears, there are just a few dangling ends that still need to be taken care of:
render_as_gtable()
, likerender_as_html()
is an internal function.Here is a small demo what it looks like:
Created on 2024-02-01 with reprex v2.1.0