public
Last active

A sample demo of R Markdown with pandoc

  • Download Gist
Makefile
Makefile
1 2 3 4 5 6 7
RMDFILE=demo-rmd-pandoc
PANDOC=~/.cabal/bin/pandoc
 
all:
Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); purl('$(RMDFILE).rmd')"
${PANDOC} --mathjax --toc -B header.html -A footer.html --bibliography refs.bib --css markdown.css -s $(RMDFILE).md -o $(RMDFILE).html
demo-rmd-pandoc.rmd
RMarkdown
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
% A little demonstration of R Markdown and Pandoc
% Author
% Date
 
``` {r setup, cache=FALSE, include=FALSE}
opts_knit$set(aliases=c(h='fig.height', w='fig.width',
cap='fig.cap', scap='fig.scap'))
opts_knit$set(eval.after = c('fig.cap','fig.scap'))
knit_hooks$set(document = function(x) {
gsub('(\\\\end\\{knitrout\\}[\n]+)', '\\1\\\\noindent ', x)
})
options(width=80)
library(latticeExtra)
trellis.par.set(custom.theme.2())
```
 
 
## A little simulation
 
Here is a basic setup to illustrate linear regression. Let's generate some data like $y = 1.1+0.8x+\varepsilon$, where $\varepsilon\sim\mathcal{N}(0;1)$:
 
``` {r simulate, cache=TRUE}
n <- 40
x <- runif(n, min=0, max=10)
y <- 1.1 + 0.8*x + rnorm(n)
```
 
and a basic scatter display of the artificial dataset
 
``` {r display, dev='quartz_png', cap='Figure caption goes here.'}
xyplot(y ~ x, type=c("p","g","r"))
```
 
Here is model output from `lm`:
 
``` {r estimate, echo=FALSE}
summary(m <- lm(y ~ x))
```
 
Of course, we can do many other fancy things, but see
**knitr**
[Chunk options and package options](http://yihui.name/knitr/options).
 
For example, if you have the `Hmisc` package (you should really have it),
you can try the following:
 
``` {r setup_birthwt, include=FALSE}
data(birthwt, package="MASS")
birthwt <- within(birthwt, {
low <- factor(low, labels=c("No","Yes"))
race <- factor(race, labels=c("White","Black","Other"))
smoke <- factor(smoke, labels=c("No","Yes"))
ui <- factor(ui, labels=c("No","Yes"))
ht <- factor(ht, labels=c("No","Yes"))
})
```
 
``` {r summary_birthwt, message=FALSE}
library(Hmisc)
f <- function(x, digits=1) c(mean=round(colMeans(x), digits=digits), range=paste(apply(x, 2, range), collapse="-"))
summary(low ~ ., data=birthwt, method="reverse",
overall=TRUE)
summary(bwt ~ ., data=birthwt, fun=f)
summary(low ~ smoke + ht + ui, data=birthwt, fun=table)
```
 
## More with Pandoc
 
Pandoc allows to customize HTML or PDF output through default or custom
theme. It is also easy to include bibliographic entries the usual way,
e.g. `@wickham11` will read @wickham11.
 
 
## References
header.html
HTML
1 2 3 4 5 6 7 8 9 10 11
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 
<!-- add this if you want to use a custom JS -->
<!--#include virtual="mathjax.html"-->
 
</head>
<body>
refs.bib
TeX
1 2 3 4 5 6 7
@Article{wickham11,
author = {Wickham, H},
title = {The Split-Apply-Combine Strategy for Data Analysis},
journal = {Journal of Statistical Software},
year = 2011,
volume = 40,
number = 1}

Tested with R version 2.15.1 (2012-06-22) and

$ pandoc --version
pandoc 1.9.3
Compiled with citeproc-hs 0.3.4, texmath 0.6.0.3, highlighting-kate 0.5.0.6.

Note: I have some Md formatting issue with latest release of pandoc (1.10).

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.