2012-12-17
In applied statistics, multiple iterations of:
How to coordinate scripts, results, drafts and final reports efficiently?
Combine source code and free text:
DE Knuth, 1981: WEB in Pascal/TeX to write TeX
Nuweb: combine any formatting/programming language
R2HTML
, odfWeave
etc.)Sweave
not updated for some timeknitr
+ pandocMarkdown: simple, readable text formatting instead of LaTeX
knitr
: powerful, flexible replacement for Sweave
pandoc
: markdown converter for many, many formats instead of pdflatex
Workflow:
.Rmd
->
.md
->
.pdf
, .docx
, .html
, .epub
, .rtf
etc..Rmd
->
.R
% A simple markdown example
% by Yours Truly
% In the year 2929
Markdown is a simple text formatting standard that aims to be human
readable. It is inspired by old-school email formatting.
It supports _italic_ and __bold__ text formatting. It supports simple
lists as in
* important item,
* other important item
which can of course be ordered and/or nested. Images can easily be
integrated, as in

pandoc -s --mathjax
PureMarkdown.md -o
PureMarkdown.html
pandoc
PureMarkdown.md -o
PureMarkdown.pdf
pandoc
PureMarkdown.md -o
PureMarkdown.docx
pandoc
PureMarkdown.md -o
PureMarkdown.rtf
pandoc
PureMarkdown.md -o
PureMarkdown.odt
pandoc
PureMarkdown.md -o
PureMarkdown.epub
A .Rmd file is essentially a markdown file with R code chunks between
triple grave accents, with chunk options in braces. Like so:
```{r}
require(MASS)
data(anorexia)
summary(anorexia)
```
Here, the `r` in braces indicates that the content should be evaluated
by R, and replaced by the result when weaving.
Usally, chunks get names, as well as extra options that regulate the
evaluation and display during weaving. Like so:
```{r myPlot, echo=FALSE, dpi=300, fig.cap="Weight gain by treatment"}
boxplot(Postwt-Prewt~Treat, anorexia)
```
Here, the chunk is called `myPlot`, the R code generating the output
is not shown, the image resolution is set to 300, and there is a
figure caption.
knitr
At the R command line:
> require(knitr)
> knit("RMarkdown.Rmd")
weaves file RMarkdown.Rmd into pure markdown to be converted as before:
pandoc -s --mathjax
RMarkdown.md -o
RMarkdown.html
pandoc
RMarkdown.md -o
RMarkdown.pdf
pandoc
RMarkdown.md -o
RMarkdown.docx
knitr
+ pandocEditor: write markdown plus code chunks
R: weave using command knit
in package knitr
At the command line: convert via pandoc
purl
in knitr
for tanglingascii
in package ascii
for tablesknitr
default is actually LaTeXknit
reports errors, does not crashknitr
, pandocFor yourself or co-analysts/developers:
For co-researchers/general reader:
xlsx
)Sexiness: low
Sexiness: negative
Analysis
for annotated codeResults
with Result.Rmd
report template
Results_20121217.docx
etc.A new way to implement LP for statistics
LP for statistics is harder than for CS
Challenge: re-think workflows to use LP efficiently
Recent version of R
Packages knitr
and ascii
(from CRAN)
Pandoc
Recommended: pdflatex
A good editor or IDE - a very personal choice (Geany, WinEdt, RStudio, Eclipse, vim…)
This presentation was written as a pure markdown file and converted to HTML via
pandoc -s -S -t slidy --mathjax LitProg_20121217.md \
-o LitProg_20121217.html
The current versions can be found at
http://www.meb.ki.se/~aleplo/LP2012/LitProg_20121217.html
and