I am trying to generate an HTML report, using knitr, based on an R script that has for loops. I want to generate markdown comments from the comments within the for loop, bu
One solution that worked for me, is provided by how to create a loop that includes both a code chunk and text with knitr in R. By using Both results='asis'
and two spaces in front of \n
at the end of each loop.
example:
Without two spaces:
```{r, results='asis'}
headers <- list("We","are","your","friends")
for (i in headers){
cat("\n##H ", i, " \n")
cat("comment",i)
}
Output (html):
As you can see, the comments and headings get messed together
Solution:
With two spaces: cat(" \n")
at the end of the loop
for (i in headers){
cat("\n##H ", i, "\n")
cat("comment",i)
cat(" \n")# <---------------------------------
}
note: cat(" \n")
needs to be at the very end, it does not work even if you plot or calculate something it in the loop.