rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
2.02k stars 205 forks source link

`opt_interactive` does not work in loops inside Quarto #1351

Open mcanouil opened 1 year ago

mcanouil commented 1 year ago

Description

Using regular print or knitr::knit_child() tricks do not work to display gt tables when opt_interactive is used.

Reproducible example

---
title: "Quarto and Interactive Tables"
format: html
---

## Interactive (not working)

```{r}
#| results: asis
#| echo: false
#| panel: tabset
library(gt)
for (i in 1:2) {
  cat(sprintf("\n\n## Tab %s\n\n", i))
  print(opt_interactive(gt(head(gtcars))))
}
#| results: asis
#| echo: false
#| panel: tabset
library(gt)
for (i in 3:4) {
  cat(sprintf("\n\n## Tab %s\n\n", i))
  cat(
    knitr::knit_child(
      quiet = TRUE,
      text = c(
        "```{r}",
        "#| results: asis",
        "#| echo: false",
        "print(opt_interactive(gt(head(gtcars))))",
        "```"
      )
    )
  )
}

Static (working)

#| results: asis
#| echo: false
#| panel: tabset
library(gt)
for (i in 1:2) {
  cat(sprintf("\n\n## Tab %s\n\n", i))
  print(gt(head(gtcars)))
}
#| results: asis
#| echo: false
#| panel: tabset
library(gt)
for (i in 3:4) {
  cat(sprintf("\n\n## Tab %s\n\n", i))
  cat(
    knitr::knit_child(
      quiet = TRUE,
      text = c(
        "```{r}",
        "#| results: asis",
        "#| echo: false",
        "print(gt(head(gtcars)))",
        "```"
      )
    )
  )
}

## Expected result

Tables should be displayed without and with `opt_interactive()`.

## Session info

- Quarto latest development version.

```sh
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.4

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Paris
tzcode source: internal

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

other attached packages:
[1] devtools_2.4.5 usethis_2.2.0 

loaded via a namespace (and not attached):
 [1] jsonlite_1.8.5    miniUI_0.1.1.1    compiler_4.3.0    renv_0.17.3-62    crayon_1.5.2      promises_1.2.0.1  Rcpp_1.0.10       stringr_1.5.0    
 [9] prompt_1.0.1      callr_3.7.3       later_1.3.1       fastmap_1.1.1     mime_0.12         R6_2.5.1          htmlwidgets_1.6.2 profvis_0.3.8    
[17] shiny_1.7.4       rlang_1.1.1       cachem_1.0.8      stringi_1.7.12    httpuv_1.6.11     fs_1.6.2          pkgload_1.3.2     memoise_2.0.1    
[25] cli_3.6.1         magrittr_2.0.3    ps_1.7.5          digest_0.6.31     processx_3.8.1    xtable_1.8-4      remotes_2.4.2     lifecycle_1.0.3  
[33] prettyunits_1.1.1 vctrs_0.6.2       glue_1.6.2        urlchecker_1.0.1  sessioninfo_1.2.2 pkgbuild_1.4.0    purrr_1.0.1       tools_4.3.0      
[41] ellipsis_0.3.2    htmltools_0.5.5
rich-iannone commented 1 year ago

Thanks for reporting this Mickaël! I recall having problems with iteration and gt tables before in R Markdown. This might generarally have something to do with that. Will have a closer look at this soon.

mcanouil commented 1 year ago

I believe the issue is not specific to Quarto, as I am expecting the same thing within RMarkdown (knitr), but did not really tested it. I simply discovered this when using Quarto since I do not use rmarkdown anymore^^

xx02al commented 5 days ago

I just stumbled across this issue while looking for something else. I now find myself with the following. If that helps in any way...

---
title: "Quarto and Interactive Tables"
format: html
---

```{r}
library(gt)
#| results: asis
#| echo: false
#| panel: tabset

for (i in 1:2) {
  cat(sprintf("\n\n## Tab %s\n\n", i))
  print(opt_interactive(gt(head(gtcars))))
}

No need for print() in child doc. Works even if table gets longer (i.e., without head() - what I found causing troubles in another situation).

With the part below, the above example from @mcanouil works, without it, it does not...!?

#| results: asis
#| echo: false
#| panel: tabset

purrr::walk(
  .x = c(3:4),
  .f = \(x) {
    cat(sprintf("\n\n## Tab %s\n\n", x))
    cat(
      knitr::knit_child(
        quiet = TRUE,
        text = c(
          "```{r}",
          "#| results: asis",
          "#| echo: false",
          "opt_interactive(gt(gtcars))",
          "gt(gtcars)",
          "```"
          )
        )
    )
    }
  )
mcanouil commented 5 days ago

Thanks, but if I put the explicit print(), it is for a reason even if indeed you might omit it in this simple example.