% 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   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