使用Knitr和texi2dvi编译多个PDF [英] Compiling multiple PDFs using knitr and texi2dvi

查看:105
本文介绍了使用Knitr和texi2dvi编译多个PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用knitr为与数据集中每个因子水平相关的数据生成单独的PDF.但是,我遇到了各种各样的错误(包括在下面),并且无法编译生成的.tex文件. 我已经尝试在主题

My goal is to use knitr to produce separate PDFs for data associated with each factor level in a data set. However, I've gotten a variety of errors (included below) and am unable to compile the .tex files that are produced. I've tried trouble shooting with the main SO post on the topic here as well as Yihui's GitHub code here, but I'm still not getting a final product. I suspect there might be other users who are also still struggling to get this right after looking at the listed resources.

这是我一直在尝试的一个虚拟示例,试图找出我在更长的脚本中遇到的错误.使用钻石"数据集:

Here is a dummy example I've been working on to attempt to isolate the errors I'm encountering in my much longer scripts. Using the "diamonds" data set:

# test.Rnw ----------------------------------------------
\documentclass[10pt]{article}
\usepackage[margin=1.15 in]{geometry}
\begin{document}

<<setup, include=FALSE, results='hide', cache=FALSE>>=
library(ggplot2)
opts_chunk$set(echo=FALSE, warning = FALSE, message = FALSE, cache = FALSE, error = FALSE,
           fig.path = paste('~/Desktop/figure/diamonds', x, sep = '')))
@

<<loaddata, echo=FALSE, message=FALSE>>=
slice <- diamonds[ diamonds$cut == x,]
@

<<test_image>>==
mybars = ggplot(slice) + 
  geom_bar(aes(x = color, stat = "bin"))
@

<<print_image, results='asis'>>==
mybars 
@
\end{document}

还有

# dispatcher.R -------------------------------------------
library(knitr)
diamonds = diamonds[diamonds$cut != "Very Good",]
# I did this ^ just in case the space caused problems in file paths. 

lapply(unique(diamonds$cut), function(x)
  knit("~/Desktop/test.Rnw",
       output=paste('~/Desktop/', x, '.tex', sep="")))

lapply(unique(diamonds$cut), function(x)  
  tools::texi2pdf(paste('~/Desktop/test/diamonds', x, '.tex',sep=""),
  clean = TRUE, quiet = TRUE, texi2dvi =  getOption("/usr/bin/texi2dvi")))

当我运行dispatcher.R时,我会获得每个切割级别的.tex文件和图像(PDFS).那太棒了!但是,我仍然没有得到完全编译的PDF.这很重要,因为具有多个块,文本,图像等的文档将需要编译为一个PDF文档.

When I run dispatcher.R I get .tex files and images (PDFS) for each cut level. That's great! However, I'm still not getting the fully compiled PDF. This is important because documents with multiple chunks, text, images etc., will need to be compiled into one PDF document.

我也尝试过使用knit2pdf,但结果相同:图像,.tex文件和没有编译的PDF.我的R控制台显示通常的编译消息,但找不到PDF.

I've also tried using knit2pdf but with the same outcome: images, .tex files, and no compiled PDFs. My R console is showing the usual compiling messages but the PDFs are nowhere to be found.

for(x in unique(diamonds$cut)){
  knit2pdf("~/Desktop/test.Rnw", 
           output=paste0('~/Desktop/diamonds', x, '.tex'))
}


processing file: ~/Desktop/test.Rnw
  |.......                                                          |  11%
  ordinary text without R code

  |..............                                                   |  22%
label: setup (with options) 
List of 3
 $ include: logi FALSE
 $ results: chr "hide"
 $ cache  : logi FALSE

  |......................                                           |  33%
  ordinary text without R code

  |.............................                                    |  44%
label: loaddata (with options) 
List of 2
 $ echo   : logi FALSE
 $ message: logi FALSE

我做错了什么?这是足够的信息吗?

What am I doing wrong? Is this enough information?

推荐答案

我原始帖子的问题主要与将乳胶指向.Rnw文件和opts_chunks()中fig.path的路径问题有关. .以下答案对我有用,并且我已经能够将其用作其他项目的模板.

The problem(s) with my original post were mostly related to path issues for to point latex to the .Rnw file and the fig.path in opts_chunks(). The following answer is working for me, and I've been able to use it as a template for other projects.

#test.R
library("knitr")
diamonds = diamonds[diamonds$cut != "Very Good",]

for(x in unique(diamonds$cut)){
  setwd("/Users/me/Desktop/thing")
  knit2pdf("test.Rnw", 
           output=paste0(x, '.tex'))
}

还有

%test.Rnw
\documentclass{article}
\usepackage[margin=.5in, landscape]{geometry}
\begin{document}

TEST TEXT! 

<<setup, include=FALSE, results='hide', cache=FALSE>>=
opts_chunk$set(echo=FALSE, warning = FALSE, message = FALSE, cache = FALSE, error = FALSE,
               fig.path = paste0('figure/', x))
library(ggplot2)
@

<<loaddata, message=FALSE>>=
slice <- diamonds[ diamonds$cut == x,]
head(slice)
@

<<test_image, echo = FALSE>>==
mybars = ggplot(slice) + 
  geom_bar(aes(x = color, stat = "bin"))
@

<<printplotscreen, results='asis'>>==
mybars
@

\end{document}

这篇关于使用Knitr和texi2dvi编译多个PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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