2015-11-02 38 views
1

我的目標是使用knitr爲與數據集中的每個因子級別相關的數據生成單獨的PDF。但是,我收到了各種錯誤(包含在下面),無法編譯生成的.tex文件。 我已經嘗試了關於主題here以及Yihui的GitHub代碼here的SO主題的疑難解答,但我仍然沒有收到最終產品。我懷疑可能還有其他用戶在查看列出的資源後仍然努力做到這一點。使用knitr和texi2dvi編譯多個PDF

這裏是一個虛擬的例子,我一直在努力嘗試隔離我遇到的更長的腳本中的錯誤。使用 「鑽石」 數據集:

# 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文檔。

我也試過使用knit2pdf,但結果相同:圖像,.tex文件和沒有編譯的PDF文件。我的R控制檯顯示通常的編譯信息,但PDF無法找到。

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 

我在做什麼錯?這是否足夠的信息?

+0

你能對你將如何使用BREW詳細點嗎? – Nancy

+0

更新:我仍在爲此工作:(如果在別人能夠解決問題之前發現它,我會報告回答。 – Nancy

回答

0

我原來發布的問題主要與路徑問題有關,以便將乳膠指向.Rnw文件和opts_chunks()中的fig.path。以下答案適用於我,並且我可以將其用作其他項目的模板。

#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}