gt
gt copied to clipboard
Generating better reprex with gt
Prework
- [x] Read and abide by gt's code of conduct and contributing guidelines.
- [x] Search for duplicates among the existing issues (both open and closed).
Proposal
I create a LOT Of reprex
examples with gt
tables. The rendered output in the viewer comes out "ok" but doesn't accurately represent the gt
table even with venue = "html"
. It also creates a very unwieldy and long output that is primarily raw HTML/CSS.
In my mind, when calling reprex
on a gt
table, it should almost always actually do:
data %>%
some_gt_code() %>%
gtsave("temp.png")
At the reprex
level, it is rendering via rmarkdown
but not including the CSS in the rendered output but rather as raw CSS:
library(gt)
head(mtcars) %>%
gt()
reprex
output:
![](https://user-images.githubusercontent.com/29187501/174364279-3c2784db-8741-43ae-89ad-471654db8d71.png)
actual gt
output which is stylized very differently.
reprex
and potential solutions
Please include a minimal reproducible example (AKA a reprex). If you've never heard of a reprex before, start by reading https://www.tidyverse.org/help/#reprex.
library(gt)
head(mtcars) %>%
gt()
and then call reprex::reprex_html()
or reprex::reprex()
While the table itself doesn't appear correctly, it also adds a massive amount of CSS classes which are captured in the details tag below.
LONG output
library(gt)
head(mtcars) %>%
gt()
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
---|---|---|---|---|---|---|---|---|---|---|
21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
Created on 2022-06-17 by the reprex package (v2.0.1)
A workaround I do commonly is save the table out to an image instead via gt::gtsave()
which adds the verbatim table as an image tag and is much more useful to provide in bug reports/reprex
. A reprex
of this style is seen below. However, the addition of gtsave()
code is "unnecessary" to the minimal functionality of the code and more about the output.
The vast majority of the reprex
or bug reports I see from users submitting gt
examples only report the code and MAYBE a manual screenshot of varying quality as not everyone thinks to do the gtsave()
hack.
Would it be possible to have a method in gt
so that when gt
is called in the context of a reprex
it would generate the image output or at least suggest to users that they include that in their code?
library(gt)
head(mtcars) %>%
gt() %>%
gtsave("out.png")
Created on 2022-06-17 by the reprex package (v2.0.1)
I believe this could be accomplished via a custom print method when rendering in the context of a reprex
.
https://bookdown.org/yihui/rmarkdown-cookbook/opts-render.html
Or possibly some interaction with screenshot.force = TRUE
, although that is currently focused on HTML widgets.
https://bookdown.org/yihui/bookdown/html-widgets.html#html-widgets
For this I think we need to upgrade webshot to webshot2 (in #621 ). But, definitely, better reprexes are needed from gt and I consider this an important issue.
I have just been informed this issue existed because I have implemented this behavior in nflplotR. See https://nflplotr.nflverse.com/reference/gt_render_image.html
My idea was to have the gt output saved as png and plotted to the viewer through magick (because my package depends on magick anyways). @jthomasmock suggested knitr instead which also is working.
It's also not only about reprexes. It's about dynamically rendering tables in package examples (see example on my pkgdown site).
However, rendering the images in examples has a downside, as chromote keeps open connections which causes R CMD check errors. I bypass them by running the examples only if the relevant environment variable is set to false and do this in the pkgdown workflow.
Dev gtExtras now has a gt_reprex_image()
function, calling reprex::reprex()
on the code below generates the following reprex:
head(mtcars) |>
gt::gt() |>
gtExtras::gt_plt_bar(mpg) |>
gtExtras::gt_reprex_image()
![](https://i.imgur.com/1xrvMSP.png)
Created on 2023-02-27 by the reprex package (v2.0.1)
In an ideal world, something like this could live in gt
proper and perhaps have a method so that when reprex()
is called on a gt pipeline, it automatically calls it?
Without this function, the output reprex is almost worthless 😭 as it would appear like this (in collapsed section below):
head(mtcars) |>
gt::gt() |>
gtExtras::gt_plt_bar(mpg)
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
---|---|---|---|---|---|---|---|---|---|---|
6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 | |
6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | |
4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 | |
6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | |
8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 | |
6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
Created on 2023-02-27 by the reprex package (v2.0.1)