【问题标题】:Compiling multiple PDFs using knitr and texi2dvi使用 knitr 和 texi2dvi 编译多个 PDF
【发布时间】:2015-11-02 20:42:15
【问题描述】:

我的目标是使用 knitr 为与数据集中每个因子级别相关的数据生成单独的 PDF。但是,我遇到了各种错误(包括在下面)并且无法编译生成的 .tex 文件。 我已经尝试使用关于主题 here 的主要 SO 帖子以及 Yihui 的 GitHub 代码 here 进行故障排除,但我仍然没有得到最终产品。我怀疑可能有其他用户在查看列出的资源后仍在努力实现这一点。

这是一个虚拟示例,我一直在努力尝试隔离我在更长的脚本中遇到的错误。使用“钻石”数据集:

# 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

我做错了什么?这些信息够不够?

【问题讨论】:

  • 您能详细说明您将如何使用 brew 吗?
  • 更新:我仍在努力解决这个问题:(如果我在其他人能够解决它之前发现它,我会报告答案。

标签: r latex knitr


【解决方案1】:

我原始帖子的问题主要与将乳胶指向 .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}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2017-10-25
    • 2013-02-09
    相关资源
    最近更新 更多