Manually combining R code and a presentation can be quite a pain. Luckily, using tools like odfWeave, Sweave and knitr, integrating documents and R code is quite painless. In this post I want to take a look at combining the knitr package with the Latex package beamer. I use the knitr package instead of the the Sweave package because it basically is a better Sweave, see this link for more information.
The basic structure of a knitr document is that you fill your text file with latex code, interspaced with section of R code like this:
|
1 2 3 |
<<name_of_chunk>>= some R code here @ |
It is custom to save this file with a .Rnw extension. Going from the Rnw to a pdf is a two step procedure: 1) run knitr, and 2) run pdflatex. First, knitr interprets the R code to produce a tex file, and then pdflatex creates the pdf. Below I posted a basic example which shows how to use knitr together with the beamer document class. The resulting pdf looks like this, and this is the knitr/latex code (Rnw file) that was the source.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
\documentclass{beamer} \begin{document} \title{A Minimal Demo of knitr} \author{Yihui Xie} \maketitle \begin{frame}[fragile] You can test if \textbf{knitr} works with this minimal demo. OK, let's get started with some boring random numbers: <<boring-random>>= set.seed(1121) (x=rnorm(20)) mean(x);var(x) @ \end{frame} \begin{frame}[fragile] The first element of \texttt{x} is \Sexpr{x[1]}. Boring boxplots and histograms recorded by the PDF device: <<boring-plots,dev=pdf,fig.width=5,fig.height=5,out.width=.45\linewidth,par=TRUE>>= ## two plots side by side (option fig.show=hold) boxplot(x) hist(x,main='') @ \end{frame} \end{document} |

You may consider publishing code via gist like https://gist.github.com/1803930 so that you can embed a code file in your post with a line of JavaScript
I’m not a github user, I primarily use bitbucket. But probably bitbucket also has a comparable service. Thanks for the tip!
My thoughts are ialsmir to Gavin’s comment, but I would like to be more specific. Write support code in .R files, write presentation and light analysis in .Rnw files. By this I mean, write complicated functions in .R files. You can invert the Sweave mentality by heavily commenting your code. Similarly, for long computations, write the analysis in a .R file and use save() to save the results. In .Rnw files write light analysis (e.g. getting descriptive statistics) and presentation (e.g. uses of the xtable package). I highly recommend the use of stopifnot() in .Rnw files to make sure the claims you make in the text match the computations. Assertions are useful for pure .Rnw writing, but can be very helpful when writing blended .R and .Rnw code.Manually rerunning analysis is painful, so I use makefiles to automate the build process. If I update analysis.R, the makefile ensures that any .Rnw files that depend on analysis.R are re-weaved. The downside to this solution is that it makes your #1 even more true.Best wishes,-Mark
It is really interesting. if we have a big R program and large number of out put (Like list, data frame) , how can one select suitable to print. or we have to write all program code in Latex file.
I recommend you take a look at some tutorials that show knitr. See the knitr websites from some starters.