2012-12-07 200 views
6

我正在使用R來分析統計數據和繪製直方圖,散點圖等。乳膠和R束?

然後我必須將所有繪圖導出爲PDF以手動將它們包含在LaTeX報告中。

我想知道是否有任何方法可以簡化這個過程?

我會很樂意寫類似:

\chapter{One} 
\begin{r} 
    qplot(...) 
\end{r} 

這樣\begin{r}\end{r}之間的代碼會產生一個情節,某個地方將它保存爲PDF和生產TeX這樣的:

\begin{figure}[ht!] 
    \includegraphics[width=1\textwidth,height=1\textheight]{/path/to/plot.pdf} 
\end{figure} 

回答

6

你想要的是knitr

該網站有lots of examples

您的文檔中,你可以這樣做

<<boring-plots, fig.width=4, fig.height=4, out.width='.4\\linewidth'>>= 
## two plots side by side (option fig.show='hold') 
par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3,las=1) 
boxplot(x) 
hist(x,main='') 
@ 

甚至設定,讓您的

\begin{r} 

\end{r} 

語法會工作。

minimal examplepdf output從上面的例子中自帶

+0

謝謝!我以前怎麼可能不知道這件事! –

+3

謝謝!你們很快。我很高興我能夠遠離SO,專注於我的針織書而不是:) –

5

Rstudio + knitr是巨大

http://www.rstudio.com/ide/docs/authoring/overview 
+0

謝謝!我正在使用RStudio。這個真的很酷。 :) –

10

看看是否可以通過5分鐘videoknitr的主頁確信:http://yihui.name/knitr/如果你只關心LaTeX,從2:54開始。

你的源代碼會是這樣的:

\chapter{One} 
<<plot, out.width='1\textwidth', out.height='1\textheight', fig.pos='!ht', fig.cap='your caption'>>= 
    qplot(...) 
@ 
+0

謝謝!它看起來非常整齊。 –