我想生成大量报告。为简单起见,假设我想创建 5 个小型 pdf 文档,其中仅包含一个循环名称向量的简单标题。
\documentclass[12pt]{article}
\newcommand{\dsfrac}[2]{\frac{\displaystyle #1}{\displaystyle #2}}
\author{Me}
\title{\Sexpr{print(namelist)}}
\maketitle
\end{document}
我将如何循环生成这些报告:
namelist <- c("Tom","Dick","Harry","John","Jacob")
提前致谢!
PS:向我展示如何定义生成的 PDF 文档的名称的奖励积分。
最佳答案
您可以在循环中调用 Sweave
,如下所示。
# Create the template file, "test.Rnw"
template <- "test.Rnw"
cat("
\\documentclass{article}
\\title{\\Sexpr{namelist[i]}}
\\begin{document}
\\maketitle
\\end{document}
", file=template)
# Parameters
namelist <- c("Tom","Dick","Harry","John","Jacob")
# Main loop: just compile the file,
# it will use the current value of the loop variable "i".
for(i in 1:length(namelist)) {
Rnw_file <- paste("test_", i, ".Rnw", sep="")
TeX_file <- paste("test_", i, ".tex", sep="")
file.copy(template, Rnw_file)
Sweave(Rnw_file)
system(paste("pdflatex --interaction=nonstopmode", TeX_file))
}
关于r - 使用 Sweave 动态生成 PDF 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738711/