3
votes

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).

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)

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}

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.

1

1 Answers

4
votes

I cannot reproduce the problem using your example and knitr 1.2 / R 3.0. I see all 10 plots as expected.

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