2
votes

I try to shift from Sweave to knitr but I am stucked with a problem to use multiple plots in one chunk. Here an example: Let this .Rnw simple file (named Essai.Rnw). Of course only one plot is shown in the pdf:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<fig=TRUE>>=
plot(1:10, exp(1:10))
plot(1:10, log(1:10))
@

\end{document}

I transform it to knitr format using:

library("knitr", lib.loc="/Library/Frameworks/R.framework/Versions/3.0/Resources/library")
Sweave2knitr("Essai.Rnw")

I edit the file to get this:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<include=FALSE>>=
require(knitr)
opts_chunk$set(concordance=TRUE)
@
<<>>=
plot(1:10, exp(1:10))
plot(1:10, log(1:10))
@
\end{document}

In Rstudio global preference, I set Weave Rnw files using Knitr and when I produce pdf file using Compile pdf button in Rstudio, i get only one plot, not the 2 as I expected.

Here is my sessionInfo()

> sessionInfo()
R version 3.0.1 Patched (2013-06-10 r62935)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.2

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.3 formatR_0.7    stringr_0.6.2  tools_3.0.1   
2
Definitive solution ! I solve the problem. I created this file part of a project in Rstudio and in the project option, this option was defined: Weave Rnw files using (Sweave). I change it to (knitr) and now all is ok. In conclusion, the option for project had priority over the global preference of Rstudio who was well defined for knitr.MarcG

2 Answers

1
votes

Not sure this is the cause of your problem but if you weave it with knitr, I think you have to remove the \SweaveOpts{concordance=TRUE} line.

1
votes

If I compile the following, I get two plots. Note the deletions I made. Your original code did not compile on my system.

\documentclass{article}
\begin{document}

<<include=FALSE>>=
opts_chunk$set(concordance=TRUE)
@
<<>>=
plot(1:10, exp(1:10))
plot(1:10, log(1:10))
@
\end{document}

Maybe read about the knitr option fig.keep= about how your plots get integrated into the output.