Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A sample demo of R Markdown with pandoc

% A little demonstration of R Markdown and Pandoc % Author % Date

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)$:

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

xyplot(y ~ x, type=c("p","g","r"))                                                                                             

Here is model output from lm:

summary(m <- lm(y ~ x))                                                                                                        

Of course, we can do many other fancy things, but see knitr Chunk options and package options.

For example, if you have the Hmisc package (you should really have it), you can try the following:

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"))                                                                                       
})                                                                                                                             
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

<!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>
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
@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}
@chlalanne
Owner

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.