R and presentations: a basic example of knitr and beamer

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:

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.

Tagged with: , ,
Posted in R stuff
5 Comments » for R and presentations: a basic example of knitr and beamer
  1. Yihui says:

    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 :)

    • Paul Hiemstra says:

      I’m not a github user, I primarily use bitbucket. But probably bitbucket also has a comparable service. Thanks for the tip!

      • Heike says:

        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

  2. V. K. Sharma says:

    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.

1 Pings/Trackbacks for "R and presentations: a basic example of knitr and beamer"
  1. [...]  R and presentations: a basic example of knitr and beamer:  Combining the knitr package with the Latex package beamer for presentation slides, instead of the the Sweave package because it basically is a better Sweave. [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">