Reputation: 6584
I am putting together a Knitr script to generate a report. The content of the report is determined dynamically at runtime, where sections of the report are generated via a loop. Ideally I would like to do both of the following in each iteration of the loop:
However, I am battling to get both of these working at the same time.
If I use the following chunk options, then I get the plots but the HTML code is formatted in a "pre" environment.
```{r echo=FALSE, fig.keep = "all", fig.show = "asis"}
for (i in 1:3) {
cat("<h2>Heading</h2>")
print(ggplot(data.frame(x = 1:10, y = 1:10), aes(x = x, y = y)) + geom_point())
}
```
If I change the chunk options then I get the raw HTML but the plots disappear, being replaced by a short text snippet indicating the path to the plot.
```{r results="asis", echo=FALSE}
for (i in 1:3) {
cat("<h2>Heading</h2>")
print(ggplot(data.frame(x = 1:10, y = 1:10), aes(x = x, y = y)) + geom_point())
}
```
Is it possible to get both working at once?
This is my first attempt to do anything remotely non-trivial with Knitr, so I am sure that there is a simple answer to this question.
Best regards, Andrew.
Upvotes: 1
Views: 704
Reputation: 1465
This seems to work at my end:
```{r results='asis', echo=F, fig.keep='all'}
for (i in 1:3) {
cat("<h2>Heading</h2>")
pl <- ggplot(data.frame(x = 1:10, y = 1:10), aes(x = x, y = y)) + geom_point()
print(pl)
}```
Upvotes: 1