2012-12-07 161 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

谢谢!它看起来非常整齐。 –