2013-11-27 43 views
3

我認爲下面的代碼可以用於在文檔的一個部分生成一個圖,但該圖可以在文檔的後面加入(例如在附錄)。Knitr - 在主文檔中生成圖片,將其呈現在附錄中

但是,最後兩個代碼塊將被重新評估,因此myData的最新定義會在兩個圖中執行。有誰知道解決這個的(除了想出一個不同的名稱myData的第二個數據對象(這真的不是一個選項))

\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} 

非常感謝!

回答

0

你可以隨時產生的地塊爲PDF格式,然後用\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} 
+0

雖然這個工作,請參閱http://stackoverflow.com/questions/15406028/suppressing-messages-in-knitr-rmarkdown/15406290#15406290 – mnel

+0

謝謝,是的,這個解決方案可以解決這個問題。但是,能夠使用knitr功能並重新使用ref.label選項可以很好,但如果不可行,那麼我將使用\ includegraphics來插入繪圖。 – user1570707

+2

您可以使用'fig.show ='hide''而不是'pdf(); dev.off()',我認爲這是不好的做法:http://i.imgur.com/jrwbX.jpg –

3

好吧,我找到了解決辦法。我沒有意識到ref.label可能需要幾個塊。

\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} 

感謝您的輸入!