Search code examples
rlatextexknitr

LaTeX and R bundle?


I am using R to analyse statistical data and plot histograms, scatter plots etc.

And then I have to export all plots as PDFs to manually include them in LaTeX report.

I wonder if there is any way for simplification of this process?

I would be happy to write something like:

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

So that the code between \begin{r} and \end{r} would generate a plot, save it somewhere as PDF and produce TeX like this:

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

Solution

  • What you want is knitr.

    The website has lots of examples

    within your document you can do something like

    <<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='')
    @
    

    Or even set it up so your

    \begin{r}
    
    \end{r}
    

    syntax would work.

    The pdf output of the minimal example from which the example above comes