Trouble printing a list of GT tables to pdf

,

Trying to render a list of GT tables to pdf using quarto. Here's a quarto doc that shows my attempts. The last one works, sort of, but includes a "NULL" for each iteration. What's the best practice for renderinglists of gt tables to pdf?

---
title: "Untitled"
format: pdf
knitr:
  opts_chunk:
    echo: true
    info: false
    message: false
    warning: false
---

```{r}
library(gt)
library(dplyr)
```

Printing Single GT Object

```{r results='asis'}

gt_object<-gt(exibble) %>% tab_header(title="Title Here", subtitle = "Subtitle Here") %>%  as_latex()

knitr::knit_print(gt_object)
```

Printing From A List Of GT Objects

```{r results='asis'}

list_of_gt_objects<-list(gt_object, gt_object, gt_object)

#this works
list_of_gt_objects[[1]] 

#this does not work
#throws an "Undefined control sequence" error
#print(list_of_gt_objects[[1]]) 

#this works
knitr::knit_print(list_of_gt_objects[[1]]) 

```

Printing Entire List Of GT Objects (lapply)

```{r results='asis'}
#this does not work
#throws an "Undefined control sequence" error

#lapply(list_of_gt_objects, knitr::knit_print)
```


Printing Entire List Of GT Objects (for loop)

```{r results='asis'}
#this does not work
#nothing is output

for(i in 1:length(list_of_gt_objects)){
  knitr::knit_print(list_of_gt_objects[[i]])
}

#this does work
#but an NULL is inserted in each iteration

for(i in 1:length(list_of_gt_objects)){
  knitr::knit_print(cat(list_of_gt_objects[[i]]))
}

```

1 Like

For this kind of things, you should use knitr::knit_child() with loops.

See topics at

calling knit_print() in your for loop won't be enough to handle the results of a gt function.