generate markdown comments within for loop

I think you can obtain what you want in knitr with the code chunk option results=”asis” that you can specify after “#+” in an R script to be passed to spin (but the code looks less “clean” than the interesting brew solution proposed by @daroczig):

#+ results="asis", echo = FALSE
for (i in 1:5) {
    cat("## This is a heading for ", i, "\n")
    cat("<!-- This is a comment for ", i, "-->\n")
    print(i)    
}

If this is test.R script and that you do spin(“test.R”), the resulting md file will look like that :

## This is a heading for  1 
<!-- This is a comment for  1 -->
[1] 1
## This is a heading for  2 
<!-- This is a comment for  2 -->
[1] 2
## This is a heading for  3 
<!-- This is a comment for  3 -->
[1] 3
## This is a heading for  4 
<!-- This is a comment for  4 -->
[1] 4
## This is a heading for  5 
<!-- This is a comment for  5 -->
[1] 5

Leave a Comment