Raw html in RMarkdown when using gt inside for loop

I'd like to construct a table within a for loop, but the html output shows raw html as opposed to rendered html. I'm thinking that maybe print() is the issue, but if I don't use print(), then nothing gets included in the html output.
Here's my code:

library(gt)
for (i in 1:2) {
 exibble %>% gt() %>% print() 
}

And here's what the output looks like:
Screen Shot 2020-10-03 at 4.51.57 PM

If you are outputing some HTML or markdown directly from the chunk, you need to use results='asis' as a chunk option.

```{r, results='asis'}
library(gt)
for (i in 1:2) {
 exibble %>% gt() %>% print() 
}
```

See https://bookdown.org/yihui/rmarkdown-cookbook/results-asis.html

gt output are also Rmardkown compatible so you can create a list with you tables, and output theme individually in chunks or inline code. Try that:

```{r}
library(gt)
gts <- lapply(1:2, function(i) exibble %>% gt())
```

`r gts[[1]]`

`r gts[[2]]`

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Thanks a lot. Much appreciated!

1 Like