Top Banner
An Introduction to knitr and RMarkdown https://github.com/sahirbhatnagar/knitr-tutorial Sahir Bhatnagar August 12, 2015 McGill Univeristy
40

An introduction to knitr and R Markdown

Aug 19, 2015

Download

Science

sahirbhatnagar
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: An introduction to knitr and R Markdown

An Introduction to knitr and RMarkdown

https://github.com/sahirbhatnagar/knitr-tutorial

Sahir Bhatnagar

August 12, 2015

McGill Univeristy

Page 2: An introduction to knitr and R Markdown

Acknowledgements

• Toby, Matthieu, Vaughn,

Ary

• Maxime Turgeon (Windows)

• Kevin McGregor (Mac)

• Greg Voisin

• Don Knuth (TEX)

• Friedrich Leisch (Sweave)

• Yihui Xie (knitr)

• John Gruber (Markdown)

• John MacFarlane (Pandoc)

• You

2/36

Page 3: An introduction to knitr and R Markdown

Disclaimer #1

I don’t work for, nor am I an author of any of these packages. I’m just a

messenger. 3/36

Page 4: An introduction to knitr and R Markdown

Disclaimer #2

• Material for this tutorial comes from many sources. For a complete

list see: https://github.com/sahirbhatnagar/knitr-tutorial

• Alot of the content in these slides are based on these two books

4/36

Page 5: An introduction to knitr and R Markdown

Objectives for today

• Create a reproducible

document (pdf, html)

5/36

Page 6: An introduction to knitr and R Markdown

Objectives for today

• Create a reproducible

document (pdf, html)

5/36

Page 7: An introduction to knitr and R Markdown

C’est parti

6/36

Page 8: An introduction to knitr and R Markdown

What?

Page 9: An introduction to knitr and R Markdown

What is needed for Reproducible research?

code

data

text

8/36

Page 10: An introduction to knitr and R Markdown

Why?

Page 11: An introduction to knitr and R Markdown

Why should we care about RR?

For Science

Standard to judge

scientific claims

Avoid duplication

Cumulative

knowledge

development

For You

Better work

habits

Better teamwork

Changes

are easier

Higher re-

search impact

10/36

Page 12: An introduction to knitr and R Markdown

Why should we care about RR?

For Science

Standard to judge

scientific claims

Avoid duplication

Cumulative

knowledge

development

For You

Better work

habits

Better teamwork

Changes

are easier

Higher re-

search impact

10/36

Page 13: An introduction to knitr and R Markdown

001-motivating-example

Page 14: An introduction to knitr and R Markdown

A Motivating Example

Demonstrate: 001-motivating-example

12/36

Page 15: An introduction to knitr and R Markdown

How?

Page 16: An introduction to knitr and R Markdown

Tools for Reproducible Research

Free and Open Source Software

• RStudio: Creating, managing, compiling documents

• LATEX: Markup language for typesetting a pdf

• Markdown: Markup language for typesetting an html

• R: Statistical analysis language

• knitr: Integrate LATEXand R code. Based on Prof. Friedrich Leisch’s

Sweave

14/36

Page 17: An introduction to knitr and R Markdown

knitr

Page 18: An introduction to knitr and R Markdown

What knitr does

LATEX example:

Report.Rnw (contains both

code and LaTeX)

Report.tex

knitr::knit(’Report.Rnw’)

Report.pdf

latex2pdf(’Report.tex’)

16/36

Page 19: An introduction to knitr and R Markdown

What knitr does

LATEX example:

Report.Rnw (contains both

code and LaTeX)

Report.tex

knitr::knit(’Report.Rnw’)

Report.pdf

latex2pdf(’Report.tex’)

16/36

Page 20: An introduction to knitr and R Markdown

Compiling a .Rnw document

The two steps on previous slide can be executed in one com-

mand:

knitr::knit2pdf()

or in RStudio:

17/36

Page 21: An introduction to knitr and R Markdown

Incorporating R code

• Insert R code in a Code Chunk starting with

<< >>=

and ending with

@

In RStudio:

18/36

Page 22: An introduction to knitr and R Markdown

Example 1: Show code and results

<<example-code-chunk-name, echo=TRUE>>=

x <- rnorm(50)

mean(x)

@

produces

x <- rnorm(50)

mean(x)

## [1] 0.031

19/36

Page 23: An introduction to knitr and R Markdown

Example 2: Tidy code

<<example-code-chunk-name2, echo=TRUE, tidy=TRUE>>=

for(i in 1:5){ print(i+3)}

@

produces

for (i in 1:5) {

print(i + 3)

}

## [1] 4

## [1] 5

## [1] 6

## [1] 7

## [1] 8

20/36

Page 24: An introduction to knitr and R Markdown

Example 2.2: don’t show code

<<example-code-chunk-name3, echo=FALSE>>=

for(i in 1:5){ print(i+3)}

@

produces

## [1] 4

## [1] 5

## [1] 6

## [1] 7

## [1] 8

21/36

Page 25: An introduction to knitr and R Markdown

Example 2.3: don’t evaluate and don’t show the code

<<example-code-chunk-name4, echo=FALSE, eval=FALSE>>=

for(i in 1:5){ print(i+3)}

@

produces

22/36

Page 26: An introduction to knitr and R Markdown

R output within the text

• Include R output within the text

• We can do that with“S-expressions”using the command

\Sexpr{. . .}

Example:

The iris dataset has \Sexpr{nrow(iris)} rows and

\Sexpr{ncol(iris)} columns

produces

The iris dataset has 150 rows and 5 columns

23/36

Page 27: An introduction to knitr and R Markdown

Include a Figure

<<fig.ex, fig.cap='Linear Regression',fig.height=3,fig.width=3>>=plot(mtcars[ , c('disp','mpg')])fit <- lm(mpg ~ disp , data = mtcars)

abline(fit,lwd=2)

@

●●● ●●●●

●●●● ●●●

●●●

●●

●● ●

● ●●

●●

100 200 300 400

1025

disp

mpg

Figure 1: Linear regression

24/36

Page 28: An introduction to knitr and R Markdown

Include a Table

<<table.ex, results='asis'>>=library(xtable)

tab <- xtable(iris[1:5,1:5],caption='Sample of Iris data')print(tab, include.rownames=FALSE)

@

library(xtable)

tab <- xtable(iris[1:5,1:5], caption = 'Sample of Iris data')print(tab, include.rownames = F)

Sepal.Length Sepal.Width Petal.Length Petal.Width Species

5.10 3.50 1.40 0.20 setosa

4.90 3.00 1.40 0.20 setosa

4.70 3.20 1.30 0.20 setosa

4.60 3.10 1.50 0.20 setosa

5.00 3.60 1.40 0.20 setosa

Table 1: Sample of Iris data

25/36

Page 29: An introduction to knitr and R Markdown

RMarkdown

Page 30: An introduction to knitr and R Markdown

Markdown: HTML without knowing HTML

27/36

Page 31: An introduction to knitr and R Markdown

R + Markdown = RMarkdown

28/36

Page 32: An introduction to knitr and R Markdown

What rmarkdown does

RMarkdown example:

Report.Rmd (contains both

code and markdown)

Report.md

knitr::knit(’Report.Rmd’)

Report.html,

Report.pdf,

Report.doc

pandoc

29/36

Page 33: An introduction to knitr and R Markdown

What rmarkdown does

RMarkdown example:

Report.Rmd (contains both

code and markdown)

Report.md

knitr::knit(’Report.Rmd’)

Report.html,

Report.pdf,

Report.doc

pandoc

29/36

Page 34: An introduction to knitr and R Markdown

Compiling a .Rmd document

The two steps on previous slide can be executed in one com-

mand:

rmarkdown::render()

or in RStudio:

30/36

Page 35: An introduction to knitr and R Markdown

Final Remarks

Page 36: An introduction to knitr and R Markdown

How to choose between LATEX and Markdown ?

math/stat symbols tecccccc

beamer presentations teccccc

customized documents tecccc

publish to journals, arXiv

quick and easy reportstkkk

use javascript libraries tekkt

interactive plots texkkkkjjt

publish to websites

LATEX

Markdown

32/36

Page 37: An introduction to knitr and R Markdown

33/36

Page 38: An introduction to knitr and R Markdown

Always Remember ...

Reproducibility ∝ 1

copy paste

34/36

Page 39: An introduction to knitr and R Markdown

Is the juice worth the squeeze?

35/36

Page 40: An introduction to knitr and R Markdown

Session Info

• R version 3.2.1 (2015-06-18), x86_64-pc-linux-gnu

• Base packages: base, datasets, graphics, grDevices, methods, stats,

utils

• Other packages: data.table 1.9.4, dplyr 0.4.1, ggplot2 1.0.1,

knitr 1.10.5, xtable 1.7-4

• Loaded via a namespace (and not attached): assertthat 0.1,

chron 2.3-45, colorspace 1.2-6, DBI 0.3.1, digest 0.6.8, evaluate 0.7,

formatR 1.2, grid 3.2.1, gtable 0.1.2, highr 0.5, magrittr 1.5,

MASS 7.3-43, munsell 0.4.2, parallel 3.2.1, plyr 1.8.3, proto 0.3-10,

Rcpp 0.12.0, reshape2 1.4.1, scales 0.2.5, stringi 0.5-5, stringr 1.0.0,

tools 3.2.1

Slides made with Beamer mtheme

36/36