问题
I want to be able to generate tabsets in my R markdown file based on a changing number of categories. I have written the following example .Rmd:
---
title: "Dynamic Tabsets"
output: html_document
---
# Graph Tabs {.tabset .tabset-pills}
```{r, results='asis'}
headers <- list('graph 1', 'graph 2', 'graph 3')
for (h in headers){
cat("##", h, '<br>', '\n')
cat('This is text for', h, '<br>')
plot.new()
plot(diffinv(rnorm(100)), type = 'o', main = h)
cat('\n', '<br>', '\n')
}
```
I want this to create a tab set with 3 tabs, each containing one of the graphs. Currently it's not working when I knit the file:
Is there any way to overcome this problem? Ideally I would like to get to a point where I can dynamically determine content that goes in an R markdown output.
回答1:
If I add an extra newline at the end of each section, I get the desired results:
```{r, results='asis'}
headers <- list('graph 1', 'graph 2', 'graph 3')
for (h in headers){
cat("##", h, '<br>', '\n')
cat('This is text for', h, '<br>')
plot.new()
plot(diffinv(rnorm(100)), type = 'o', main = h)
cat('\n', '<br>', '\n\n')
}
```
Remember that Markdown often requires a full blank line between different elements.
来源:https://stackoverflow.com/questions/49725591/dynamically-control-number-of-tabsets-in-r-markdown