gt icon indicating copy to clipboard operation
gt copied to clipboard

Printing tables in a loop fails with TeX/PDF output (quarto)

Open cswingle opened this issue 3 years ago • 0 comments

Description

When printing a tables in a loop I get the proper output when choosing html format (via quarto), but the tables are not printing correctly in pdf format.

Reproducible example

Here's an example quarto document (loop_test.qmd):

Here are tables of mtcars by cylinder.

```{r}
#| echo: false
#| output: asis

library(tidyverse)
library(gt)

plot_by_cyl <- function (cyl_) {
  data <- mtcars %>% filter(cyl == cyl_)
  print(gt(data))
  # print(knitr::kable(data)). # <-- this works
}

walk(
  mtcars %>% pull(cyl) %>% unique() %>% sort(),
  plot_by_cyl
)
```

Expected result

When the document is rendered with quarto render loop_test.qmd --to html the individual tables show up as expected. When rendering with quarto render loop_test.qmd --to pdf, the tables do not show up. Instead each cell is printed on its own line.

When I comment out print(gt(data)) and uncomment print(knitr::kable(data)) in the code, I get the same tables in both HTML and PDF formats.

Session info

  • Quarto build from the latest HEAD on 2022-05-09 (commit aa1022ff6)
  • XeTeX, Version 3.141592653-2.6-0.999993 (TeX Live 2021)
  • R session info
R version 4.1.2 (2021-11-01)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

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

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

other attached packages:
 [1] gt_0.5.0.9000   knitr_1.39      forcats_0.5.1   stringr_1.4.0  
 [5] dplyr_1.0.9     purrr_0.3.4     readr_2.1.2     tidyr_1.2.0    
 [9] tibble_3.1.7    ggplot2_3.3.6   tidyverse_1.3.1

loaded via a namespace (and not attached):
 [1] cellranger_1.1.0 pillar_1.7.0     compiler_4.1.2   dbplyr_2.1.1    
 [5] tools_4.1.2      digest_0.6.29    jsonlite_1.8.0   lubridate_1.8.0 
 [9] lifecycle_1.0.1  gtable_0.3.0     pkgconfig_2.0.3  rlang_1.0.2     
[13] reprex_2.0.1     rstudioapi_0.13  DBI_1.1.2        cli_3.3.0       
[17] xfun_0.30        haven_2.5.0      fastmap_1.1.0    xml2_1.3.3      
[21] withr_2.5.0      httr_1.4.3       fs_1.5.2         generics_0.1.2  
[25] vctrs_0.4.1      hms_1.1.1        grid_4.1.2       tidyselect_1.1.2
[29] glue_1.6.2       R6_2.5.1         fansi_1.0.3      readxl_1.4.0    
[33] tzdb_0.3.0       modelr_0.1.8     magrittr_2.0.3   htmltools_0.5.2 
[37] backports_1.4.1  scales_1.2.0     ellipsis_0.3.2   rvest_1.0.2     
[41] assertthat_0.2.1 colorspace_2.0-3 utf8_1.2.2       stringi_1.7.6   
[45] munsell_0.5.0    broom_0.8.0      crayon_1.5.1   

cswingle avatar May 09 '22 19:05 cswingle