我有一个像这样的 R markdown 文档:
The following graph shows a histogram of variable x:
```{r}
hist(x)
```
我想引入一个循环,这样我就可以对多个变量做同样的事情。假设是这样的:
for i in length(somelist) {
output paste("The following graph shows a histogram of somelist[[" , i, "]]")
```{r}
hist(somelist[[i]])
```
这可能吗?
PS:更大的计划是创建一个程序,该程序将遍历数据框并自动为每列生成适当的摘要(例如直方图、表格、箱线图等)。然后,该程序可用于自动生成一个 Markdown 文档,其中包含您在查看第一个数据时要做的探索性分析。
这就是你想要的吗?
---
title: "Untitled"
author: "Author"
output: html_document
---
```{r, results='asis'}
for (i in 1:2){
cat('\n')
cat("#This is a heading for ", i, "\n")
hist(cars[,i])
cat('\n')
}
```
这个答案或多或少是从here https://stackoverflow.com/questions/17178831/generate-markdown-comments-within-for-loop.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)