reprex
reprex copied to clipboard
reprex for html tables like gt
Please briefly describe your problem and what output you expect. If you have a question, please don't use this form. Instead, ask on https://stackoverflow.com/ or https://community.rstudio.com/.
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.
While I'm not sure if this is something to be solved at the reprex level or the gt level, I wanted to start here with an issue. 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 would perhaps be nice to at least throw a warning/message if gt_tbl class object is found as the output is neither accurate nor very useful. See a basic output below:
library(gt)
head(mtcars) %>%
gt()
reprex output:
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 things to do the gtsave() hack.
Would it be possible to have a method in gt or in reprex 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)
This has a lot of echoes from #404, which is about doing something similar for rgl output.
That ended up being taken care of entirely inside rgl, as it's really an rgl <--> knitr matter and then reprex just inherits that behaviour.
I suspect a similar solution would be the way to go for gt.