且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 R 中使用 knitr 创建一个包含代码块和文本的循环

更新时间:2021-07-12 09:53:20

您可以使用 cat() 将 Markdown 嵌入循环中.

You can embed the markdown inside the loop using cat().

注意:您需要设置 results="asis" 以将文本呈现为 Markdown.请注意:您将需要在 新行字符前面有两个空格才能让 knitr 在有情节的情况下正确呈现降价.

Note: you will need to set results="asis" for the text to be rendered as markdown. Note well: you will need two spaces in front of the new line character to get knitr to properly render the markdown in the presence of a plot out.

# Monthly Air Quality Graphs
```{r pressure,fig.width=6,echo=FALSE,message=FALSE,results="asis"}

attach(airquality)
for(i in unique(Month)) {
  cat("  
###",  month.name[i], "Air Quaility  
")
  #print(plot(airquality[airquality$Month == i,]))
  plot(airquality[airquality$Month == i,])
  cat("  
")
}
```