Mer*_*rik 41 markdown r r-markdown
我有一个像这样的R降价文件:
The following graph shows a histogram of variable x:
```{r}
hist(x)
```
Run Code Online (Sandbox Code Playgroud)
我想介绍一个循环,所以我可以为多个变量做同样的事情.假设有这样的东西:
for i in length(somelist) {
output paste("The following graph shows a histogram of somelist[[" , i, "]]")
```{r}
hist(somelist[[i]])
```
Run Code Online (Sandbox Code Playgroud)
这甚至可能吗?
PS:更大的计划是创建一个程序,该程序将遍历数据框并自动为每列生成适当的摘要(例如直方图,表格,箱形图等).然后,该程序可用于自动生成降价文档,其中包含在查看第一个数据的数据时要进行的探索性分析.
Ale*_*lex 44
这可能是你想要的吗?
---
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')
}
```
Run Code Online (Sandbox Code Playgroud)
正如已经提到的,任何循环都需要位于代码块中。为直方图指定标题可能比为每个直方图添加一行文本作为标题更容易。
```{r}
for i in length(somelist) {
title <- paste("The following graph shows a histogram of", somelist[[ i ]])
hist(somelist[[i]], main=title)
}
```
Run Code Online (Sandbox Code Playgroud)
但是,如果您想创建多个报告,请查看此线程。
其中还有此示例的链接。
看来当从脚本内进行渲染调用时,环境变量可以传递到 Rmd 文件。
因此,另一种选择可能是使用 R 脚本:
for i in length(somelist) {
rmarkdown::render('./hist_.Rmd', # file 2
output_file = paste("hist", i, ".html", sep=''),
output_dir = './outputs/')
}
Run Code Online (Sandbox Code Playgroud)
然后你的 Rmd 块将如下所示:
```{r}
hist(i)
```
Run Code Online (Sandbox Code Playgroud)
免责声明:我没有对此进行测试。