how to create a loop that includes both a code chunk and text with knitr in R

前端 未结 2 1818
粉色の甜心
粉色の甜心 2020-11-27 16:10

I am trying to figure out how to create a loop that inserts some text into the rmarkdown file, and then produces the graph or table that corresponds to that header. The foll

2条回答
  •  盖世英雄少女心
    2020-11-27 16:49

    As mentioned here, you could also make use of the pander package:

    # Monthly Air Quality Graphs
    ```{r pressure2, fig.width=6, echo=FALSE, message=FALSE, results="asis"}
    library(pander)
    for (i in unique(airquality$Month)) {
       # Inserts Month titles
       pander::pandoc.header(month.name[i], level = 3)
       # Section contents
       plot(airquality[airquality$Month == i,])
       # adding also empty lines, to be sure that this is valid Markdown
       pander::pandoc.p('')
       pander::pandoc.p('')
    }
    ```
    

提交回复
热议问题