r - 使用 Sweave 动态生成 PDF 报告

标签 r latex sweave

我想生成大量报告。为简单起见,假设我想创建 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/

相关文章:

python - 我怎样才能动画化代数运算,最好是用Python

Git 在一行内 merge

r - 提取 R 函数的参数以在 knit 中使用

sweave - pgfSweave 删除 tikz 生成的图表

r - logLik.lm() : Why does R use (p + 1) instead of p for degree of freedom?

r - 从 xml2 和 rvest 子集 data.frame 时出错

r - 使用 rvest 进行网络抓取 : filtering through paginanation

r - 使用ggplot在R中具有非整数频率的多个直方图

latex - 如何使用MathJax渲染增强矩阵?

r - 使用 Graphviz 使用 R 包 'sem' 绘制路径图