Dynamically control number of tabsets in R Markdown

断了今生、忘了曾经 提交于 2019-12-11 03:24:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!