1

I am trying to print gtExtras::gt_plt_winloss tables in a for loop in a paged HTML RMarkdown document. Outside of a loop, the tables are rendering as expected. Inside of a loop, the table preview is as expected in the console but prints to PDF partially in raw and partially in rendered HTML. I would appreciate any help figuring out why the rendering is going wrong in the loop!

---
title: example
output: 
  pagedown::html_paged:
    toc: true
    toc_depth: 1
    self_contained: true
    number_sections: false
knit: pagedown::chrome_print
paged-footnotes: true
---
set.seed(37)
data_in <- dplyr::tibble(
  grp = rep(c("A", "B", "C"), each = 10),
  wins = sample(c(0,1,.5), size = 30, prob = c(0.45, 0.45, 0.1), replace = TRUE)
) %>%
  dplyr::group_by(grp) %>%
  dplyr::summarize(wins=list(wins), .groups = "drop")

win_table <- data_in %>%
  gt() %>%
  gt_plt_winloss(wins)

Renders as expected in console and in PDF:

```{r, results = 'asis'}
win_table %>% 
  tab_header("no loop")
```

winloss table outside of loop

Renders fine in console but messed up in PDF:

```{r, results = "asis"}
for(i in 1:2) {
  win_table %>%
    tab_header(paste("looped attempt", i)) %>% 
    print()
}
```

problems rendering winloss inside of loop

cxinya
  • 25
  • 4

1 Answers1

0

We may use

   ```{r, results = "asis"}
  for(i in 1:2) {
    out <- win_table %>%
       tab_header(paste("looped attempt", i))
        cat(knitr::knit_print(out))
    
     }
     ```

-output enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you! This worked but I am also trying to include a header above each table as part of the loop. I previously had `cat(paste("## loop attempt", i))` before the table code in each loop but now this first `cat()` isn't working. I haven't been able to figure out a way to combine the header and gt in a single `cat()`. Is there a way to have both? – cxinya Jan 09 '23 at 19:45
  • @cxinya your comment is not clear to me. Can you show in your post what exactly youwanted – akrun Jan 09 '23 at 20:25
  • 1
    Apologies @akrun -- putting together a reprex helped me figure out my own problem. Thanks very much for the help! – cxinya Jan 09 '23 at 20:46