在R Markdown中动态控制选项卡集的数量

War*_*ner 2 markdown r r-markdown

我希望能够根据类别数量的变化在我的R markdown文件中生成选项卡集。我写了以下示例.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')
}
```
Run Code Online (Sandbox Code Playgroud)

我希望它创建一个包含3个选项卡的选项卡集,每个选项卡均包含一个图形。目前,当我编织文件时,它不起作用:

在此处输入图片说明

有什么办法可以克服这个问题?理想情况下,我希望可以动态确定R markdown输出中包含的内容。

Mar*_*ius 5

如果在每个部分的末尾添加额外的换行符,则会得到预期的结果:

```{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')
}
```
Run Code Online (Sandbox Code Playgroud)

请记住,降价通常需要在不同元素之间使用完全空白行。