Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A sample demo of R Markdown with pandoc
% 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)
## 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
[Chunk options and package 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}
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",
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<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"-->
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
author = {Wickham, H},
title = {The Split-Apply-Combine Strategy for Data Analysis},
journal = {Journal of Statistical Software},
year = 2011,
volume = 40,
number = 1}

This comment has been minimized.

Copy link
Owner Author

even4void commented Oct 25, 2012

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, highlighting-kate

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.