2016-07-13 45 views
-1

我正在使用R studio版本0.99.485。我必须根据一个输入矢量做许多报告,所以我决定在R Markdown(R studio)中编写循环。我只给出部分代码:创建许多文档

```{r forensis, results='asis', echo=FALSE} 
load(file = "E:/data/R/Forensic_reports/fdata.RData") 
for (i in 1:length(osobni_podaci$Oib)) { 
    cat(" \n### UPIT ZA OIB: ", oibreq[i], ' \n') 
    cat(' \n### STATUS OIB-A \n') 
    cat('Status: ',ifelse(oib_status$X_status[i] == 1, 'Aktivan', 'Neaktivan'), ' \n') 
    cat(' \n### OSNOVNI PODACI \n') 
    cat("Ime: ", osobni_podaci$Ime[i], ' \n') 
} 
``` 

因此,对于某些向量中的每一个我,我都会写相同结构的报告。

如果我像这样执行代码,它将返回一个文档中的所有报告,但我希望拥有与报告一样多的html文档。

在循环结束时,我需要在for循环结束时添加什么内容才能在每次循环结束时将报告保存为文档?

+0

R会创建一个'pdf/html,jpeg文件',并开始放置所有的东西,除非它被修复或被告知要做一个新的东西。与数字一样,您可以执行dev.off()或dev.next()。 所以我会怀疑有类似的Markdown,或者只是在创建HTML文件 –

+0

@Jan Sila据我了解,你不可能从rmarkdown r chunk做多个html文档。我不知道如何将dev.off()或dev.next()嵌入到代码中。 – Mislav

+0

我不是那个意思,但类似的东西?当我登录电脑时,我也会尝试Google –

回答

0

我发现这里的答案:https://github.com/petrelharp/r-markdown-tutorial

有了这样一个.rmd:

--- 
title: "Visualization for `r input.file`" 
date: "`r date()`" 
--- 

```{r setup, echo=FALSE} 
if (!file.exists(input.file)) { stop("Can't find file.") } 
xy <- read.table(input.file) 
``` 

The file `r input.file` 
has `r nrow(xy)` observations: 

```{r plotit} 
plot(height ~ age, col=type, data=xy) 
legend("topleft", pch=1, legend=levels(xy$type), col=1:nlevels(xy$type)) 
``` 

的R这样的脚本:

library(knitr) 
owd <- setwd("examples/thedata") # determines where output will go 
opts_knit$set(root.dir=".") # determines where code is evaluated 
file.names <- list.files(".",".*tsv") 
for (input.file in file.names) { 
    output.file <- gsub(".tsv$",".viz.html",input.file) 
    knit2html("../simple-template.Rmd", output=output.file, quiet=TRUE) 
} 

和一堆.tsv格式输入存储在评估R脚本的目录中的文件。