jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
195 stars 27 forks source link

Error in rendering using gt_plt_bar_stack() for multiple gt objects #93

Closed jacciz closed 1 year ago

jacciz commented 1 year ago

Description

I have a function that returns multiple gt objects so to render an HTML. When I use gt_plt_bar_stack(), the table gets messed up. The header and the first line renders just fine. After the first line on the table, the next line shifts over to the left. Some raw HTML gets printed above. There must be an issue with bad HTML getting injected or something.

Looked at HTML <td headers="mpg" class="gt_row gt_right">21.0</td> got removed (first cell in second line) after the <tr> tag.

Reproducible example

#| results: asis

x=mtcars |> dplyr::mutate(l = list(c(1, 2))) |> gt::gt() |> gtExtras::gt_plt_bar_stack(l)
purrr::walk(list(x), print)

Expected result

Columns should line up.

Session info

R version 4.3.0 (2023-04-21 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 [4] LC_NUMERIC=C LC_TIME=English_United States.utf8

time zone: America/Chicago tzcode source: internal

attached base packages: [1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached): [1] gtable_0.3.3 dplyr_1.1.2 compiler_4.3.0 rematch2_2.1.2 tidyselect_1.2.0 psych_2.3.3 xml2_1.3.4 gtExtras_0.4.5
[9] parallel_4.3.0 scales_1.2.1 fastmap_1.1.1 readxl_1.4.2 lattice_0.21-8 ggplot2_3.4.2 R6_2.5.1 paletteer_1.5.0
[17] generics_0.1.3 knitr_1.43 tibble_3.2.1 munsell_0.5.0 pillar_1.9.0 rlang_1.1.1 utf8_1.2.3 xfun_0.39
[25] cli_3.6.1 magrittr_2.0.3 digest_0.6.31 grid_4.3.0 fontawesome_0.5.1 rstudioapi_0.14 cowplot_1.1.1 lifecycle_1.0.3
[33] nlme_3.1-162 vctrs_0.6.2 mnormt_2.1.1 glue_1.6.2 cellranger_1.1.0 gt_0.9.0 fansi_1.0.4 colorspace_2.1-0 [41] purrr_1.0.1 tools_4.3.0 pkgconfig_2.0.3 htmltools_0.5.5

jthomasmock commented 1 year ago

Howdy, I'm not able to follow along with the issue. I was able to successfully print the table.

I've minimally modified the reprex to not be as long of a table.

x <- mtcars |>
  head() |> 
  dplyr::mutate(l = list(c(1, 2))) |>
  gt::gt() |>
  gtExtras::gt_plt_bar_stack(l)

purrr::walk(list(x), print)

image

Can you provide a screenshot of the issue you're facing? And provide a bit more context on why/how you're using the "multiple gt objects so to render an HTML"?

jacciz commented 1 year ago

Interesting. I was automating reports, creating a series of charts for each survey question (each use a likert scale). I wrote a function that outputs multiple gt objects and would use that function for each survey question. Here's a screenshot of the knitted html. I love gt/gtExtras - been my go-to packages for anytime I make tables. image

jthomasmock commented 1 year ago

What might be happening is that the RMarkdown/Quarto doc itself is breaking the HTML or the list elements are being mangled.

If you can provide a minimal reproducible example of a report that breaks I can dive in a bit further.

jacciz commented 1 year ago

Yeah, the issue happens with both RMarkdown/Quarto. The reprex above breaks, but I see it works for you. Attached is the html inside a zip. I tried on my personal computer and it still breaks. I just decided to use a function to render each table. reprex.zip

jthomasmock commented 1 year ago

OK, so I have been able to reproduce the issue with a purrr::walk() + print().

Quarto/RMarkdown are mangling the HTML, and I'm not 100% sure of the fix offhand.

However, by saving the elements to a list, and then printing them individually, the HTML is not mangled. I may ask the Quarto team given there is some existing problems that have been resolved: https://github.com/quarto-dev/quarto-cli/issues/4419

---
format: html
---

```{r}
gt_tab_fn <- function(df){
  tab_out <- df |> 
    head() |> 
  dplyr::mutate(l = list(c(1, 2))) |>
  gt::gt() |>
  gtExtras::gt_plt_bar_stack(l, labels = c("Group 1", "Group 2")) |>
    gt::as_raw_html()
}
#| results: asis
tabs <- purrr::map(list(mtcars, mtcars), gt_tab_fn)
tabs[[1]]
tabs[[2]]


![image](https://github.com/jthomasmock/gtExtras/assets/29187501/4fef9341-393e-44ed-86d6-78139c964b7f)
jthomasmock commented 1 year ago

@rich-iannone does this strike you as an error in my gtExtras code or something in gt/quarto? On gt v0.9 and Quarto v1.4.193

jthomasmock commented 1 year ago

Ok, so I was able to fix this with the below reprex and note the use of:

::: {style="all:initial;"}
```{r}
<SOME GT CODE>

:::


Let me know if that works for you and we can close this issue.

![image](https://github.com/jthomasmock/gtExtras/assets/29187501/06cf52ab-ca00-40d2-aeaa-595fb88a2dc8)

Full snippet below:

format: html

::: {style="all:initial;"}

library(gt)
library(gtExtras)
library(dplyr)

ex_df <- dplyr::tibble(
  x = c("Example 1","Example 1",
        "Example 1","Example 2","Example 2","Example 2",
        "Example 3","Example 3","Example 3","Example 4","Example 4",
        "Example 4"),
  measure = c("Measure 1","Measure 2",
              "Measure 3","Measure 1","Measure 2","Measure 3",
              "Measure 1","Measure 2","Measure 3","Measure 1","Measure 2",
              "Measure 3"),
  data = c(30, 20, 50, 30, 30, 40, 30, 40, 30, 30, 50, 20)
)

tab_df <- ex_df %>%
  group_by(x) %>%
  summarise(list_data = list(data))

ex_tab <- tab_df %>%
  gt() %>%
  gt_plt_bar_stack(column = list_data)
gt::as_raw_html(ex_tab)

:::

jacciz commented 1 year ago

Yes, this works. Your comment in the previous comment is also a solution.