如何从单个数据框创建具有不同内容的多个PDF?

时间:2017-04-11 15:43:30

标签: r latex knitr

问题

我想从单个数据框中编织多个PDF。因此我尝试了各种解决方案,但我在R,R Studio,LaTex,knitr中的知识非常有限,所以我无法适应某些解决方案,最后我自己尝试了。我实际上认为我的代码绝对不是你实际用来实现我想要实现的方式。所以,请随时告诉我可以/应该改进的地方和地点。

我真的很感激一些帮助。我一直在谷歌搜索几个小时,如果你能推荐我任何教程/指南/解释,我也将不胜感激。我甚至不知道从哪里开始。

当前状态:已解决

代码

main.R

for(i in 1:nrow(mtcars)) {
  g_title <- rownames(mtcars)[i]
  knit2pdf(input  = "main.Rnw", 
           output = paste0("output\\", g_title, ".pdf"), 
           quiet  = FALSE, 
           envir  = parent.frame())
}

template.Rnw

\documentclass{article}
\usepackage[ngerman]{babel}

\begin{document}
\begin{titlepage}
  Titlepage
\end{titlepage}

\tableofcontents
\newpage

\section{Topic 1}
\newpage

\section{Topic 2}    

\end{document}

解决方案方法

全局变量

我试图创建由for循环改变的全局变量。然后,这些变量以函数的形式用于.Rnw文件中。由于未知错误,我无法使其正常工作。

<。> .R文件中的代码:

printPlot <- function() {
  print(g_plot)
}

for(i in 1:nrow(mtcars)) {
  g_title <- rownames(mtcars)[i]
  g_plot  <- ggplot(mtcars[i,], aes(x = cyl, y = disp) ) + 
             geom_point()
  knit2pdf(input  = "main.Rnw", 
           output = paste0("output\\", g_title, ".pdf"), 
           quiet  = FALSE, 
           envir  = parent.frame())
}

.Rnw文件中的代码:

<<>>=
printPlot()
@

错误:

创建了PDF,但其内容混乱了。您可以在“当前状态”下的图像中看到它。

我还收到一些错误/警告信息,例如:

  

警告讯息:       1:运行命令'“C:\ Users \ Marc \ AppData \ Local \ Programs \ MIKTEX~1.9 \ miktex \ bin \ x64 \ texify.exe”   --quiet --pdf“Mazda RX4.pdf” - max-iterations = 20 -I“C:/PROGRA~1/R/R-33~1.2/share/texmf/tex/latex”-I   “C:/PROGRA~1/R/R-33~1.2/share/texmf/bibtex/bst”'的状态为1       2:运行命令'“C:\ Users \ Marc \ AppData \ Local \ Programs \ MIKTEX~1.9 \ miktex \ bin \ x64 \ texify.exe”   --quiet --pdf“Mazda RX4 Wag.pdf” - max-iterations = 20 -I“C:/PROGRA~1/R/R-33~1.2/share/texmf/tex/latex”-I   “C:/PROGRA~1/R/R-33~1.2/share/texmf/bibtex/bst”'状态为1

生成文件

我刚刚第一次阅读有关makefile的内容。也许这有助于解决问题。

如果我说得对,makefile与Markdown一起使用,而不是直接与LaTex一起使用。这似乎是性能的巨大损失。这一点对我来说非常重要,所以我会尝试找到另一种解决方案。

其他SO问题

在大多数情况下,我尝试调整代码,但只是失败了,因为我缺少了解给定解决方案方法的知识。

1 个答案:

答案 0 :(得分:2)

从这个问题来看,我并不完全确定预期的产出,但是概念很清楚。虽然任务本身很简单,但很多事情都可能出错。

代码:

<强> code.R

library(knitr)
library(ggplot2)

dir.create(path = "output/")
opts_knit$set(base.dir = "output/")

for(i in 1:nrow(mtcars)) {
  filename <- rownames(mtcars)[i]
  knit(input  = "template.Rnw", output = paste0("output/", filename, ".tex"))
  tools::texi2pdf(paste0("output/", filename, ".tex"), clean = TRUE)
  file.copy(from = paste0(filename, ".pdf"), to = paste0("output/", filename, ".pdf"))
  # file.remove(paste0(filename, ".pdf")) # this will DELETE filename.pdf from the current working directory (should be safe because we just created this file)
}

<强> template.Rnw

\documentclass{article}
\begin{document}
<<>>=
ggplot(mtcars[i,], aes(x = cyl, y = disp) ) + geom_point()
@
\end{document}
  • 我们需要设置base.dir,因为当前工作目录比创建文档的目录高一级。这将导致错误的数字路径:knitr在figure/中生成图,但它们应该在output/figure/中。因此,编译将失败。
  • 由于某种原因,knit2pdf无法编译生成的中间TEX文件。因此,我使用knit生成TEX文件,然后使用tools::texi2pdf将此文件编译为PDF。

请注意code.R中的变量如何对模板文档中的代码可见。这就是i可以使用template.Rnw的原因。