## Building a presentation, report or paper in R

If you need to build a presentation, obviously you have following options:

• Powerpoint alike presentation
• Online engines
• LaTex

The first two are beloved by business people and the third one is widely used in academia. The objective of the first group is shiny presentation, contrary to the second where asceticism and demand for automation are top priorities. However, if you are data scientist or any other data specialist with a need to build an automated report, then you know, that LaTex is just wrong.
LaTex allows you to build a shiny presentation or outstanding paper, however it can take light years to build something useful for beginners . If you never tried LaTex here is an example of the monster – you literally have to code a document or presentation:

\documentclass{article}
\title {Investment strategy}
\author {Dzidorius Martinaitis}
\begin{document}
\maketitle

So, what do you do, if you need only 1% of all LaTex features and a report/document needs to be build automatically? Turns out, that HTML little brother Markdown is saving the world. Markdown(.md) source files are easy to read and easy to write and you can convert it into .html, .pdf, .docx, .tex or any other format. There are many ways to do conversion, however I use Pandoc utility. By the way this post was written in markdown in Vim and you can check the source file.

However, the nicest thing about Markdown is integration with R. You can build your report in one file, where R code would be embed in Markdown. Knitr package will help you to convert R code into Markdown simply by calling this piece of code:

require(knitr);
knit('workshop.Rmd', 'workshop.md');

Below you will find an excerpt of .Rmd file which is mix of R and Markdown:

Get the data
===

{r results=asis,comment=NA, message=FALSE}
names=sapply(tweets,function(x)x$screenName) rez=(aggregate(names,list(factor(names)),length)) rez=rez[order(rez$x),]
colnames(rez)=c('name','count')
options(xtable.type = 'html')
require(xtable)
xtable(t(tail(rez,6)))


Plot top10 tweeters
===
{r topspam, figure=TRUE,fig.cap=''}
barplot(tail(rez$count,10),names.arg=as.character(tail(rez$name,10)),cex.names=.7,las=2)


Here is a workshop presentation which contains the example above – I built it for Haxogreen hackers camp and source code can be found on gitHub.

1. ### David Tellet said,

August 2, 2012 @ 13:35

So instead of \section{} you use ===. And instead of Sweave you use knitr. That seems to just make Markdown a “monster” of another flavor. And I have yet to see an example of Markdown that even approaches the quality of a LaTeX document. Sorry, I will stick with LaTeX and Sweave, nothing “monstrous” about them.

2. ### G. Grothendieck said,

August 2, 2012 @ 17:39

@David, pandoc can generate TeX formats: LaTeX, ConTeXt and LaTeX Beamer slides so you can still get TeX output. Also as long as your LaTeX is sufficiently simple pandoc can translate in the other direction too. As long as you are satisfied with the subset of LaTeX that markdown gives you then markdown seems beneficial in terms of reducing the learning curve that TeX/LaTeX would otherwise have. On the other hand, if you already know TeX/LaTeX reasonably well then markdown has less benefit since you are already over the learning curve anyways.

3. ### nick said,

August 2, 2012 @ 22:44

what is a good place to learn about markdown, knitr, making pdf slides, making webpages with embedded R analysis etc? I looked at knitr website and the tutorial seems a little advanced. Whats a good starting tutorial?

4. ### Dzidorius Martinaitis said,

August 2, 2012 @ 23:12

@nick this might help:
http://jeromyanglim.blogspot.com/2012/05/getting-started-with-r-markdown-knitr.html

5. ### Vincent said,

August 4, 2012 @ 23:57