使用与knitr循环产生多个PDF报告…需要一点帮助,让我在驼峰
首先,我必须承认,我对knitr和可重复分析的概念很陌生,但是我可以看到它在改进我目前的工作stream程(其中包括大量复制到word文档中)上的潜力。
我经常不得不按小组(医院在这个例子中)生成多个报告,在每个医院内,可能会有许多不同的病房,我正在报告一个结果。 以前我用R语言把所有的图表和分析都用R语言进行,然后复制/粘贴工作就开始了。 然而,读完这篇文章后( Sweave会自动生成许多PDF文件? ),这让我希望我可以跳过许多步骤,从R直接通过Rnw / knitr进行报告。
然而,在尝试一下之后,我发现有些东西没有完全解决(因为Rn中的R环境似乎没有认识到我正试图传递给它的循环variables?)。
## make my data Hospital <- c(rep("A", 20), rep("B", 20)) Ward <- rep(c(rep("ICU", 10), rep("Medicine", 10)), 2) Month <- rep(seq(1:10), 4) Outcomes <- rnorm(40, 20, 5) df <- data.frame(Hospital, Ward, Month, Outcomes) ## Here is my current work flow-- produce all plots, but export as png and cut/paste for(hosp in unique(df$Hospital)){ subgroup <- df[ df$Hospital == hosp,] for(ward in unique(subgroup$Ward)){ subgroup2 <- subgroup[subgroup$Ward == ward,] savename <- paste(hosp, ward) plot(subgroup2$Month, subgroup2$Outcomes, type="o", main=paste("Trend plot for", savename)) } } # followed by much copy/pasting ## Here is what I'm trying to go for using knitr library(knitr) for (hosp in unique(df$Hospital)){ knit("C:file.path\\testing_loops.Rnw", output=paste('report_', Hospital, '.tex', sep="")) } ## With the following *Rnw file ## start *.Rnw Code \documentclass[10pt]{article} \usepackage[margin=1.15 in]{geometry} <<loaddata, echo=FALSE, message=FALSE>>= Hospital <- c(rep("A", 20), rep("B", 20)) Ward <- rep(c(rep("ICU", 10), rep("Medicine", 10)), 2) Month <- rep(seq(1:10), 4) Outcomes <- rnorm(40, 20, 5) df <- data.frame(Hospital, Ward, Month, Outcomes) subgroup <- df[ df$Hospital == hosp,] @ \begin{document} <<setup, echo=FALSE >>= opts_chunk$set(fig.path = paste("test", hosp , sep="")) @ Some infomative text about hospital \Sexpr{hosp} <<plots, echo=FALSE >>= for(ward in unique(subgroup$Ward)){ subgroup2 <- subgroup[subgroup$Ward == ward,] # subgroup2 <- subgroup2[ order(subgroup2$Month),] savename <- paste(hosp, ward) plot(subgroup2$Month, subgroup2$Outcomes, type="o", main=paste("Trend plot for", savename)) } @ \end{document} ## To be then turned into pdf with this tools::texi2pdf("C:file.path\\report_A.tex", clean = TRUE, quiet = TRUE)
试图运行我的knit()代码块后,我得到这个错误:
Error in file(con, "w") : invalid 'description' argument
当我查看要创build* .tex文件的目录时,我可以看到医院A的2个pdf图(没有B),也没有医院特定的* .tex文件编织成pdf。 预先感谢您提供的任何帮助!
您不需要重新定义.Rnw
文件中的数据,我认为警告来自于您将输出名称与Hospital
( Hospital
的完整vector)而不是hosp
(循环索引)。
按照你的例子, testingloops.Rnw
会
\documentclass[10pt]{article} \usepackage[margin=1.15 in]{geometry} <<loaddata, echo=FALSE, message=FALSE>>= subgroup <- df[ df$Hospital == hosp,] @ \begin{document} <<setup, echo=FALSE >>= opts_chunk$set(fig.path = paste("test", hosp , sep="")) @ Some infomative text about hospital \Sexpr{hosp} <<plots, echo=FALSE >>= for(ward in unique(subgroup$Ward)){ subgroup2 <- subgroup[subgroup$Ward == ward,] # subgroup2 <- subgroup2[ order(subgroup2$Month),] savename <- paste(hosp, ward) plot(subgroup2$Month, subgroup2$Outcomes, type="o", main=paste("Trend plot for", savename)) } @ \end{document}
和驱动程序R文件将是公正的
## make my data Hospital <- c(rep("A", 20), rep("B", 20)) Ward <- rep(c(rep("ICU", 10), rep("Medicine", 10)), 2) Month <- rep(seq(1:10), 4) Outcomes <- rnorm(40, 20, 5) df <- data.frame(Hospital, Ward, Month, Outcomes) ## knitr loop library("knitr") for (hosp in unique(df$Hospital)){ knit2pdf("testingloops.Rnw", output=paste0('report_', hosp, '.tex')) }
好问题! 这适用于您在问题中提供的其他位。 请注意,我用x
代替了你的hosp
。 我打电话给你的Rnw
文件test.rnw
# input data Hospital <- c(rep("A", 20), rep("B", 20)) Ward <- rep(c(rep("ICU", 10), rep("Medicine", 10)), 2) Month <- rep(seq(1:10), 4) Outcomes <- rnorm(40, 20, 5) df <- data.frame(Hospital, Ward, Month, Outcomes) # generate the tex files, one for each hospital in df library(knitr) lapply(unique(df$Hospital), function(x) knit("C:\\emacs\\test.rnw", output=paste('report_', x, '.tex', sep=""))) # generate PDFs from the tex files, one for each hospital in df lapply(unique(df$Hospital), function(x) tools::texi2pdf(paste0("C:\\emacs\\", paste0('report_', x, '.tex')), clean = TRUE, quiet = TRUE))
我用lapply
和anonymous函数取代了你的循环,这些函数通常被认为是更加R
。
在这里你可以看到我在rnw
文件中用x
replace了rnw
:
\documentclass[10pt]{article} \usepackage[margin=1.15 in]{geometry} <<loaddata, echo=FALSE, message=FALSE>>= Hospital <- c(rep("A", 20), rep("B", 20)) Ward <- rep(c(rep("ICU", 10), rep("Medicine", 10)), 2) Month <- rep(seq(1:10), 4) Outcomes <- rnorm(40, 20, 5) df <- data.frame(Hospital, Ward, Month, Outcomes) subgroup <- df[ df$Hospital == x,] @ \begin{document} <<setup, echo=FALSE >>= opts_chunk$set(fig.path = paste("test", x , sep="")) @ Some informative text about hospital \Sexpr{x} <<plots, echo=FALSE >>= for(ward in unique(subgroup$Ward)){ subgroup2 <- subgroup[subgroup$Ward == ward,] # subgroup2 <- subgroup2[ order(subgroup2$Month),] savename <- paste(x, ward) plot(subgroup2$Month, subgroup2$Outcomes, type="o", main=paste("Trend plot for", savename)) } @ \end{document}
结果是两个tex文件(report_A.tex,report_B.tex),四个PDF(A1,A2,B1,B2)和两个PDF(报告_A.pdf,report_B.pdf),每个都带有数字在他们中。 那是你以后的事吗?