针织衫怪异的数字行为 [英] knitr strange out of control behavior with figures
问题描述
我的数字行为很奇怪.在某些块中,knitr通过单个绘图函数生成两个图形,并且从"chunkname"生成的图形分别命名为"chunkname1.pdf"和"chunkname2.pdf".有时,第一个只是与第二个相同大小的空白图形,有时是无法打开的文件.解决方法是,设置fig.keep='last'
选项.但是,现在我想显示来自同一块的两个图并将fig.keep
设置为其默认值,knitr会生成3个PDF图形文件,其中第一个是损坏的文件(一个无法打开).
I get strange behavior with figures. In some chunks knitr generates two figures from a single plot function, and the figures generated from 'chunkname' are named 'chunkname1.pdf' and 'chunkname2.pdf'. Sometimes, the first one is just a blank figure of the same size as the second one, and sometimes it is a file which cannot be open. As a work around, I set the fig.keep='last'
option. But now I would like to display two plots from a same chunk and setting fig.keep
to its default value, knitr generates 3 PDF figure files with the first one a corrupted file (one which can not be open).
我在Win7 64下具有knitr版本1.2(2013-04-10),R 3.0.0 64bit(r62697 2013-04-30).这是我正在使用的设置:
I have knitr version 1.2 (2013-04-10), R 3.0.0 64bit (r62697 2013-04-30) under Win7 64. Here are the settings I am using:
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
).
I have not been able to reproduce it with a simple example. However, trying to create such an example I discovered other strange results. In the example below, only plots 1, 2 and 10 are correctly displayed, while all other don't show up (the PDF files are not generated and consistently the \includegraphics
are not generated in the .tex file.)
\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的预期结果.
Even stranger, compiling the same file from within R-Studio, I get the expected result for plot 6 in addition to 1,2 and 10.
推荐答案
我无法使用您的示例和knitr
1.2/R 3.0来重现该问题.我看到了所有的10个地块.
I cannot reproduce the problem using your example and knitr
1.2 / R 3.0. I see all 10 plots as expected.
对于损坏的PDF数据,这似乎是knitr
开发版本中已修复的错误.你可以尝试
For the corrupted PDF figures, it seems to be a bug that has been fixed in the development version of knitr
. You can try
install.packages('knitr', repos = 'http://www.rforge.net/', type = 'source')
这篇关于针织衫怪异的数字行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!