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!