generate markdown comments within for loop

后端 未结 3 1123
慢半拍i
慢半拍i 2020-11-28 08:20

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 08:58

    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.

提交回复
热议问题