I need to generate multiple htmlwidgets
within a loop in an RMarkdown document. This is accomplished using htmltools::tagList
. The following snippet will generate HTML output with 2 level 2 headings and a datatable in each section. The only problem is that I specified toc: true
but though tags$h2(headers[i])
will render a level 2 headings on the page, those headings are not picked up by the table of contents. Is there a way get these headings in the TOC?
---
output:
html_document:
toc: true
---
## level 2 heading 0 - this one in TOC
```{r}
library(DT)
library(htmltools)
headers <- c("level 2 heading 1", "level 2 heading 2")
html_tags <- vector(mode = "list", length = 2*length(headers))
for (i in 1:length(headers)) {
html_tags[[(2*i)-1]] <- tags$h2(headers[i])
html_tags[[(2*i)]] <- list(list(datatable(iris)))
}
tagList(html_tags)
```
Note that in the result below the heading generated using "##" shows up in the table of contents, but the headings from the tagList
call, though they render correctly in the document, are now in the table of contents.
tagList
using "##" in markdown syntax – Matt Pollock