3

I thought that the code below could be used to generate a plot in one part of a document, but that the plot could be included later in the document (e.g. in the appendix).

However, the last two code chunks are re-evaluated, and the latest definition of myData is therefore executed in both plots. Does anyone know of a solution to this (apart from coming up with a different name than myData for the second data object (which really is not an option))

\documentclass[a4paper,11pt]{article}
\begin{document}

<<TestData1,echo=FALSE>>=
myData <- as.data.frame(cbind(xvar=1:10, yvar = 1:10))
@

<<TestPlot1,include=FALSE>>=
plot(myData)
@

<<TestData2,echo=FALSE>>=
myData <- as.data.frame(cbind(xvar=1:10, yvar = 10:1))
@

<<TestPlot2,include=FALSE>>=
plot(myData)
@
<<APPTestPlot1,dependson='TestPlot1',ref.label="TestPlot1",fig.cap="figCap1",fig.caps="figCaps",fig.env="figure",fig.pos="htb",fig.width=6,fig.height=6,out.width="0.5\\textwidth",include=TRUE,echo=FALSE>>=
@
<<APPTestPlot2,dependson='TestPlot2',ref.label="TestPlot2",fig.cap="figCap2",fig.caps="figCap2s",fig.env="figure",fig.pos="htb",fig.width=6,fig.height=6,out.width="0.5\\textwidth",include=TRUE,echo=FALSE>>=
@
\end{document}

Many thanks!

user1570707
  • 113
  • 1
  • 6

2 Answers2

3

Ok, I found a solution. I didn't realise that ref.label could take several chunks.

\documentclass[a4paper,11pt]{article}
\begin{document}

<<TestData1,echo=FALSE>>=
myData <- as.data.frame(cbind(xvar=1:10, yvar = 1:10))
@

<<TestPlot1,include=FALSE,echo=FALSE>>=
plot(myData)
@

<<TestData2,echo=FALSE>>=
myData <- as.data.frame(cbind(xvar=1:10, yvar = 10:1))
@

<<TestPlot2,include=FALSE,echo=FALSE>>=
plot(myData)
@

\section{Appendix}
<<APPTestPlot1,ref.label=c("TestData1","TestPlot1"),fig.cap="figCap1",fig.env="figure",fig.pos="htb",fig.width=6,fig.height=6,out.width="0.5\\textwidth",include=TRUE,echo=FALSE>>=
@

<<APPTestPlot2,ref.label=c("TestData2","TestPlot2"),fig.cap="figCap2",fig.env="figure",fig.pos="htb",fig.width=6,fig.height=6,out.width="0.5\\textwidth",include=TRUE,echo=FALSE>>=
@
\end{document}

Thanks for your input!

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
user1570707
  • 113
  • 1
  • 6
0

You can always just generate the plots as pdf, then show them with \includegraphics:

\documentclass[a4paper,11pt]{article}
\begin{document}
\SweaveOpts{concordance=TRUE}

<<TestData1, echo=FALSE>>=
myData <- as.data.frame(cbind(xvar=1:10, yvar = 1:10))
@

<<TestPlot1,include=FALSE, echo=FALSE>>=
pdf('plot1.pdf')
plot(myData)
dev.off()
@

<<TestData2,echo=FALSE>>=
myData <- as.data.frame(cbind(xvar=1:10, yvar = 10:1))
@

<<TestPlot2,include=FALSE, echo=FALSE>>=
pdf('plot2.pdf')
plot(myData)
dev.off()
@

\section{Appendix}

\includegraphics{plot1.pdf}
\includegraphics{plot2.pdf}

\end{document}
Marius
  • 58,213
  • 16
  • 107
  • 105
  • While this does work, See http://stackoverflow.com/questions/15406028/suppressing-messages-in-knitr-rmarkdown/15406290#15406290 – mnel Nov 28 '13 at 00:54
  • Thanks, yes this solution would solve the problem. However, it would be nice to be able to use the knitr functionality and reuse the chunk with the ref.label option, but if that is not possible then I will use \includegraphics to insert the plot. – user1570707 Nov 28 '13 at 07:43
  • 2
    You can use `fig.show='hide'` instead of `pdf(); dev.off()`, which I consider as bad practice: http://i.imgur.com/jrwbX.jpg – Yihui Xie Nov 30 '13 at 03:57