knitr奇怪的失控行为与数字

时间:2013-05-20 11:49:02

标签: r knitr

我对数字的行为很奇怪。在一些块中,knitr从单个绘图函数生成两个数字,并且从> chunkname'生成的数字生成。命名为' chunkname1.pdf'和' chunkname2.pdf'。有时,第一个只是一个与第二个相同大小的空白数字,有时它是一个无法打开的文件。作为一种解决方法,我设置了fig.keep='last'选项。但是现在我想显示来自同一块的两个图并将fig.keep设置为其默认值,knitr生成3个PDF图形文件,第一个是损坏的文件(一个无法打开)。

我在Win7 64下有knitr版本1.2(2013-04-10),R 3.0.0 64bit(r62697 2013-04-30)。以下是我正在使用的设置:

knit_hooks$set(par = function(before, options, envir) {
  if (before) par(mar = c(4, 4, .1, .1))
})

opts_knit$set(concordance=TRUE, self.contained=TRUE)

opts_chunk$set(fig.path="figSW/SW-", keep.source=FALSE, 
   verbose=TRUE,  message=FALSE)

我用一个简单的例子无法重现它。然而,试图创建这样一个例子,我发现了其他奇怪的结果。在下面的示例中,只显示了图1,2和10,而其他所有图表都没有显示(未生成PDF文件,并且在.tex文件中始终未生成\includegraphics。 )

\documentclass[12pt,a4paper]{article}
 \begin{document}
Plot 1
<<test-t, echo=TRUE, results="asis", fig.width=10, fig.height=5>>=
x = c(1,2,3)
y = c(1,3,2)
plot(x,y)
@
Plot 2\\
<<test-tt, echo=FALSE, results="hide", fig.width=10, fig.height=5>>=
plot(x,y)
@
\newpage
Plot 3
<<test-s, echo=TRUE, results="asis", fig.width=10, fig.height=5>>=
x = c(1,2,3)
y = c(1,3,2)
plot(x,y)
@
Plot 4\\
<<test-ss, echo=FALSE, results="hide", fig.width=10, fig.height=5>>=
plot(x,y)
@
\newpage
Plot 5
<<test-r, echo=TRUE, results="asis", fig.width=10, fig.height=5>>=
x = c(1,2,3)
y = c(1,3,2)
plot(x,y)
@
Plot 6\\
<<test-rr, echo=FALSE, results="hide", fig.width=10, fig.height=5>>=
plot(x,y)
@
\newpage
Plot 7
<<test-v, echo=TRUE, results="asis", fig.width=10, fig.height=5>>=
x = c(1,2,3)
y = c(1,3,2)
plot(x,y)
@
Plot 8\\
<<test-vv, echo=FALSE, results="hide", fig.width=10, fig.height=5>>=
plot(x,y)
@
\newpage
Plot 9
<<test-u, echo=TRUE, results="asis", fig.width=10, fig.height=5>>=
x = c(1,2,3)
y = c(1,3,2)
plot(x,y)
@
Plot 10\\
<<test-uu, echo=FALSE, results="hide", fig.width=10, fig.height=5>>=
plot(x,y)
@
\end{document}

更奇怪的是,在R-Studio中编译同一个文件,除了1,2和10之外,我得到了图6的预期结果。

1 个答案:

答案 0 :(得分:4)

我无法使用您的示例和knitr 1.2 / R 3.0重现该问题。我按预期看到了所有10个情节。

对于损坏的PDF数据,它似乎是knitr的开发版本中已修复的错误。你可以尝试

install.packages('knitr', repos = 'http://www.rforge.net/', type = 'source')
相关问题