如何从单个数据框中创建具有不同内容的多个PDF? [英] How to create multiple PDFs with different content from a single data frame?

查看:61
本文介绍了如何从单个数据框中创建具有不同内容的多个PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to knit multiple PDFs from a single data frame. Therefore I've tried various solutions, but my knowledge in R, R Studio, LaTex, knitr is very limited so I wasn't able to adapt some solution approaches and finally tried it on my own. I actually think my code is absolutely not the way you actually use to achieve what I want to achieve. So, please feel free to tell me where and what I can/should improve.

我真的很感谢您的帮助.我已经搜寻了几个小时,如果您能向我推荐任何教程/指南/说明,我也将不胜感激.我什至不知道从哪里开始.

I would be really grateful for some help. I've been googling for hours now and I would also appreciate if you could recommend me any tutorial/guide/explanation. I don't even know where to start.

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文件中使用.由于未知错误,我无法正常工作.

Solution Approaches

Global Variables

I tried to create global variables which are altered by a for loop. These variables are then used in the .Rnw file in form of a function. I wasn't able to get this working due unknown errors.

.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文件中的代码:

Code in the .Rnw file:

<<>>=
printPlot()
@

错误:

已创建PDF,但其内容混乱.您可以在当前状态"下的图像中看到它.

The PDFs are created, but their contents are messed up. You can see it in the image under 'Current State'.

我还会收到几条错误/警告消息,例如:

I also receive several error/warning messages, e.g.:

警告消息: 1:运行命令"C:\ Users \ Marc \ AppData \ Local \ Programs \ MIKTEX〜1.9 \ miktex \ bin \ x64 \ texify.exe" --quiet --pdf马自达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马自达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

Warning messages: 1: running command '"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"' had status 1 2: running command '"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"' had status 1

MakeFile

我只是第一次阅读有关makefile的内容.也许这可以帮助解决问题.

MakeFile

I just read the first time about makefile. Maybe this could help solving the problem.

如果我正确的话,makefile将与Markdown一起使用,而不是与LaTex直接一起使用.这似乎是性能上的巨大损失.这一点对我来说很重要,因此我将尝试寻找另一种解决方案.

If I got it right makefile is used with Markdown and not directly with LaTex. This seems to be a massive loss in performance. This point is quite important to me, so I will try to find another solution.

在大多数情况下,我尝试修改代码,但由于没有足够的知识来理解给定的解决方案方法而失败了.

In most of the cases I tried to adapt the code, but simply failed, because I am missing knowledge to understand the given solution approaches.

  • R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop?
  • Using loops with knitr to produce multiple pdf reports… need a little help to get me over the hump
  • Can Sweave produce many pdfs automatically?

推荐答案

从问题上来说,我对预期的输出并不完全确定,但是其中的概念很明确.而且,尽管任务本身非常简单,但是令人惊讶的是,许多事情还是会出错.

From the question, I'm not entirely sure about the expected output, but there concept is clear. And although the task itself is quite simple, surprisingly many things can go wrong.

代码:

code.R

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

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.
    • We need to set base.dir because the current working directory is one level above the directory where the document is created. This would lead to wrong figure paths: knitr produces the plots in figure/ but they should be in output/figure/. Consequently, compilation will fail.
    • For some reason knit2pdf cannot compile the generated intermediate TEX file. Therefore I use knit to produce a TEX file and then tools::texi2pdf to compile this file to PDF.
    • 请注意模板文档中的代码如何查看code.R中的变量.这就是为什么i可以在template.Rnw中使用的原因.

      Note how variables from code.R are visible to the code in the template document. That's why i can be used in template.Rnw.

      这篇关于如何从单个数据框中创建具有不同内容的多个PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆