alexander keth
alexander keth

Reputation: 1435

Interactive headers R markdown

I would like to add interactive headers to a markdown document.

Assume we have the following markdown document:

---
title: "Non-interactive header"
output: 
   html_document:
      toc: true
---

## 1 plot
```{r, echo=FALSE}
plot(1, 1)
```

## 2 plot
```{r, echo=FALSE}
plot(1, 1)
```

This will create a html document with 2 headers called "1 plot" and "2 plot". Each header is followed by the plot. However, I would like to interactively create the headers based on a variable. Following the suggestions found here (http://biochemistri.es/modular-workbook) I came up with this:

---
title: "Interactive header"
output: 
   html_document:
      toc: true
---

```{r, echo=FALSE, results = 'asis'}
for (i in paste(1:10, "plot")) {
  cat(paste("##", i), sep = "\n")
}
```

At first glance this works great. The html document consists of 10 headers called 1plot, 2 plot and so on. Unfortunately, as soon as I add code to the headers the plot is only shown after the 2nd header and subsequent headers are not shown as header anymore.

---
title: "Interactive header"
output: 
   html_document:
      toc: true
---

```{r, echo=FALSE, results = 'asis'}
for (i in paste(1:10, "plot")) {
  cat(paste("##", i), sep = "\n")
  plot(1, 1)
}
```

So the question is: How can I add r-code to each interactive header?

Upvotes: 1

Views: 371

Answers (1)

Nick
Nick

Reputation: 46

I've found that this could be achieved adding two line breaks after the plot.

---
title: "Interactive header"
output: 
   html_document:
      toc: true
---

```{r, echo=FALSE, results = 'asis'}
for (i in 1:10) {
  cat(paste("## Plot", i), sep = "\n")
  plot(i, 1)
  cat("\n\n")
}
```

Upvotes: 1

Related Questions