使用 knitr 和 texi2dvi 编译多个 PDF
Compiling multiple PDFs using knitr and texi2dvi
我的目标是使用 knitr 为与数据集中每个因子水平关联的数据生成单独的 PDF。但是,我遇到了各种错误(包括在下面)并且无法编译生成的 .tex 文件。 我已经尝试使用主 SO post 解决主题 here as well as Yihui's GitHub code here 的问题,但我仍然没有得到最终产品。 我怀疑可能有其他在查看列出的资源后仍在努力解决此问题的用户。
这是一个虚拟示例,我一直在努力找出我在更长的脚本中遇到的错误。使用 "diamonds" 数据集:
# 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
我做错了什么?这些信息足够吗?
我原来的 post 的问题主要与将乳胶指向 .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}
我的目标是使用 knitr 为与数据集中每个因子水平关联的数据生成单独的 PDF。但是,我遇到了各种错误(包括在下面)并且无法编译生成的 .tex 文件。 我已经尝试使用主 SO post 解决主题 here as well as Yihui's GitHub code here 的问题,但我仍然没有得到最终产品。 我怀疑可能有其他在查看列出的资源后仍在努力解决此问题的用户。
这是一个虚拟示例,我一直在努力找出我在更长的脚本中遇到的错误。使用 "diamonds" 数据集:
# 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
我做错了什么?这些信息足够吗?
我原来的 post 的问题主要与将乳胶指向 .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}