Compiling multiple PDFs using knitr and texi2dvi

拈花ヽ惹草 提交于 2019-12-11 10:33:48

问题


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}

And

# 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")))

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.

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?


回答1:


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'))
}

And

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


来源:https://stackoverflow.com/questions/33486496/compiling-multiple-pdfs-using-knitr-and-texi2dvi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!