cxinya
cxinya

Reputation: 25

gtExtras winloss table in loop rendering with partially raw, partially rendered HTML

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

Upvotes: 1

Views: 91

Answers (1)

akrun
akrun

Reputation: 887118

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

Upvotes: 0

Related Questions