SlideShare a Scribd company logo
1 of 43
Download to read offline
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Reproducible Research
An Introduction to knitr
Sahir Rai Bhatnagar1
May 28, 2014
1https://github.com/sahirbhatnagar/knitr-tutorial
1 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Acknowledgements
• Dr. Erica Moodie
• Maxime Turgeon
(Windows)
• Kevin McGregor (Mac)
• Greg Voisin
• Don Knuth (TEX)
• Friedrich Leisch (Sweave)
• Yihui Xie (knitr)
• You
2 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Disclaimer #1
• Feel free to Ask questions
• Interrupt me often
• You don’t need to raise your hand to speak
3 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Disclaimer #2
I don’t work for, nor am I an author of any of these packages. I’m
just a messenger.
4 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Disclaimer #3
• 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
5 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Eat Your Own Dog Food
• These slides are reproducible
• Source code: https://github.com/sahirbhatnagar/knitr-
tutorial/tree/master/slides
6 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Main objective for today
7 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
What is Science Anyway?
8 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
What is Science Anyway?
According to the American Physical Society:
Science is the systematic enterprise of gathering knowledge about the
universe and organizing and condensing that knowledge into testable
laws and theories. The success and credibility of science are
anchored in the willingness of scientists to expose their ideas and
results to independent testing and replication by other scientists
8 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
RR: A Minimum Standard to Verify Scientific
Findings
9 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
RR: A Minimum Standard to Verify Scientific
Findings
Reproducible Research (RR) in Computational Sciences
The data and the code used to make a finding are available and they
are sufficient for an independent researcher to recreate the finding
9 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Why should we
care about RR?
For Science
Standard to judge
scientific claims
Avoid duplication
Cumulative
knowledge
development
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
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 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
A Motivating Example
Demonstrate: 001-motivating-example
Survey: https://www.surveymonkey.com/s/CDVXW3C
11 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Tools for Reproducible Research2
Free and Open Source Software
• RStudio: Creating, managing, compiling documents
• LATEX: Markup language for typesetting a document
• R: Statistical analysis language
• knitr: Integrate LATEXand R code. Based on Prof. Friedrich
Leisch’s Sweave
2http://onepager.togaware.com/
12 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Comparison
Figure 1 : Comparison
• LATEX has a greater learning
curve
• Many tasks are very tedious
or impossible (most cases) to
do in MS Word or Libre Office
13 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
The Philosophy behind LATEX
Figure 2 : Adam Smith, author of
The Wealth of Nations (1776), in
which he conceptualizes the
notion of the division of labour
Division of Labour
Composition and logical structuring
of text is the author’s specific
contribution to the production of a
printed text. Matters such as the
choice of the font family, should
section headings be in bold face or
small capitals? Should they be flush
left or centered? Should the text be
justified or not? Should the notes
appear at the foot of the page or at
the end? Should the text be set in
one column or two? and so on, is the
typesetter’s business
14 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
The Genius Behind LATEX
Figure 3 : The TEX project was started in 1978 by Donald Knuth
(Stanford). He planned for 6 months, but it took him nearly 10 years to
complete. Coined the term “Literate programming”: mixture of code and
text segments that are “human” readable. Recipient of the Turing Award
(1974) and the Kyoto Prize (1996).
15 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Integrated Development Environment (IDE)
16 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Integrated Development Environment (IDE)
Demonstrate: Explore RStudio
16 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
What knitr does
LATEX example:
Report.Rnw
(contains both
code and markup)
Report.tex
knitr::knit(’Report.Rnw’)
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
What knitr does
LATEX example:
Report.Rnw
(contains both
code and markup)
Report.tex
knitr::knit(’Report.Rnw’)
Report.pdf
latex2pdf(’Report.tex’)
17 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Compiling a .Rnw document
The two steps on previous slide can be executed in one
command:
knitr::knit2pdf()
or in RStudio:
18 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Incorporating R code
• Insert R code in a Code Chunk starting with
<< >>=
and ending with
@
In RStudio:
19 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Example 1
<<example-code-chunk-name, echo=TRUE>>=
library(magrittr)
rnorm(50) %>% mean
@
produces
library(magrittr)
rnorm(50) %>% mean
## [1] 0.031
20 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Example 2
<<example-code-chunk-name2, echo=TRUE, tidy=TRUE>>=
for(i in 1:5){ (i+3) %>% print}
@
produces
for (i in 1:5) {
(i + 3) %>% print
}
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
21 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Example 2.2
<<example-code-chunk-name3, echo=FALSE>>=
for(i in 1:5){ (i+3) %>% print}
@
produces
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
22 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Example 2.3
<<example-code-chunk-name4, echo=FALSE, eval=FALSE>>=
for(i in 1:5){ (i+3) %>% print}
@
produces
Demonstrate: Try it yourself
23 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
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
24 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Include a Figure
<<fig.ex, fig.cap='Linear Regression',fig.height=3,fig.width=3>>=
plot(mtcars[ , c('disp','mpg')])
lm(mpg ~ disp , data = mtcars) %>%
abline(lwd=2)
@
100 200 300 400
1025
disp
mpg
Figure 4 : Linear regression
25 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Include a Table
<<table.ex, results='asis'>>=
library(xtable)
iris[1:5,1:5] %>%
xtable(caption='Sample of Iris data') %>%
print(include.rownames=FALSE)
@
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
26 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Minimum Working Example
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/002-
minimum-working-example
27 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Extracting output from Regression Models
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/003-
model-output
28 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Figures
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/004-
figures
29 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Beamer Presentations
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/005-
beamer-presentation
30 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Changing one Parameter in an Analysis
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/006-
sensitivity-analysis-one-parameter
31 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Changing Many Parameters in an Analysis
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/007-
sensitivity-analysis-many-parameters
32 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Large Documents
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/008-
large-documents
33 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
HTML Reports
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/009-
rmarkdown
34 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
HTML Presentations
https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/010-
rmarkdown-presentation
35 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
36 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Always Remember ...
Reproducibility ∝
1
copy paste
37 / 38
RR: Intro to
knitr
Reproducible
Research
What?
Why?
001-motivating-
example
Getting Started
LATEX
RStudio
knitr
Examples
002-minimum-
working-example
003-model-
output
004-figures
005-beamer-
presentation
006-sensitivity-
analysis-one-
parameter
007-sensitivity-
analysis-many-
parameters
008-large-
documents
009-rmarkdown
010-rmarkdown-
presentation
Final Remarks
Is the juice worth the squeeze?
38 / 38

More Related Content

Similar to Reproducible Research: An Introduction to knitr

Lightweight Design (Composites) - Americas ATC 2015 Workshop
Lightweight Design (Composites) - Americas ATC 2015 WorkshopLightweight Design (Composites) - Americas ATC 2015 Workshop
Lightweight Design (Composites) - Americas ATC 2015 Workshop
Altair
 
Track g test strategy - delta
Track g   test strategy - deltaTrack g   test strategy - delta
Track g test strategy - delta
chiportal
 

Similar to Reproducible Research: An Introduction to knitr (20)

resumelrs_jan_2017
resumelrs_jan_2017resumelrs_jan_2017
resumelrs_jan_2017
 
To Mock or Not To Mock
To Mock or Not To MockTo Mock or Not To Mock
To Mock or Not To Mock
 
Quick Acting Couplers - Legris Parker นิวเมติก.com
Quick Acting Couplers - Legris Parker นิวเมติก.comQuick Acting Couplers - Legris Parker นิวเมติก.com
Quick Acting Couplers - Legris Parker นิวเมติก.com
 
Design and Analysis of Electric Vehicle Battery Fixture
Design and Analysis of Electric Vehicle Battery FixtureDesign and Analysis of Electric Vehicle Battery Fixture
Design and Analysis of Electric Vehicle Battery Fixture
 
Final Report SEC3
Final Report SEC3Final Report SEC3
Final Report SEC3
 
Lightweight Design (Composites) - Americas ATC 2015 Workshop
Lightweight Design (Composites) - Americas ATC 2015 WorkshopLightweight Design (Composites) - Americas ATC 2015 Workshop
Lightweight Design (Composites) - Americas ATC 2015 Workshop
 
Xilinx vs Intel (Altera) FPGA performance comparison
Xilinx vs Intel (Altera) FPGA performance comparison Xilinx vs Intel (Altera) FPGA performance comparison
Xilinx vs Intel (Altera) FPGA performance comparison
 
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016
Scott Clark, Co-Founder and CEO, SigOpt at MLconf SF 2016
 
MLConf 2016 SigOpt Talk by Scott Clark
MLConf 2016 SigOpt Talk by Scott ClarkMLConf 2016 SigOpt Talk by Scott Clark
MLConf 2016 SigOpt Talk by Scott Clark
 
IRJET- Spring Testing Machine
IRJET- Spring Testing MachineIRJET- Spring Testing Machine
IRJET- Spring Testing Machine
 
Technical Lessons Learned Turning the Agile Dials to Eleven!
Technical Lessons Learned Turning the Agile Dials to Eleven!Technical Lessons Learned Turning the Agile Dials to Eleven!
Technical Lessons Learned Turning the Agile Dials to Eleven!
 
2020 Sciencetech Presentation
2020 Sciencetech Presentation2020 Sciencetech Presentation
2020 Sciencetech Presentation
 
Track g test strategy - delta
Track g   test strategy - deltaTrack g   test strategy - delta
Track g test strategy - delta
 
bolts 8.8
bolts 8.8bolts 8.8
bolts 8.8
 
IRJET- Design and Development of a Bandsaw Machine Roller Bracket for Wei...
IRJET-  	  Design and Development of a Bandsaw Machine Roller Bracket for Wei...IRJET-  	  Design and Development of a Bandsaw Machine Roller Bracket for Wei...
IRJET- Design and Development of a Bandsaw Machine Roller Bracket for Wei...
 
Design Optimization of Roller Chain Link Plate used in Sugar Industry
Design Optimization of Roller Chain Link Plate used in Sugar IndustryDesign Optimization of Roller Chain Link Plate used in Sugar Industry
Design Optimization of Roller Chain Link Plate used in Sugar Industry
 
Cnd labguide
Cnd labguideCnd labguide
Cnd labguide
 
Internal laboratory scope
Internal laboratory scopeInternal laboratory scope
Internal laboratory scope
 
Developments In Precision Positioning Stages with High Speed Range
Developments In Precision Positioning Stages with High Speed RangeDevelopments In Precision Positioning Stages with High Speed Range
Developments In Precision Positioning Stages with High Speed Range
 
Evaluating Static Analysis of the Damper Grommets for Compressor
Evaluating Static Analysis of the Damper Grommets for CompressorEvaluating Static Analysis of the Damper Grommets for Compressor
Evaluating Static Analysis of the Damper Grommets for Compressor
 

More from sahirbhatnagar

Analysis of DNA methylation and Gene expression to predict childhood obesity
Analysis of DNA methylation and Gene expression to predict childhood obesityAnalysis of DNA methylation and Gene expression to predict childhood obesity
Analysis of DNA methylation and Gene expression to predict childhood obesity
sahirbhatnagar
 

More from sahirbhatnagar (11)

Strong Heredity Models in High Dimensional Data
Strong Heredity Models in High Dimensional DataStrong Heredity Models in High Dimensional Data
Strong Heredity Models in High Dimensional Data
 
Methods for High Dimensional Interactions
Methods for High Dimensional InteractionsMethods for High Dimensional Interactions
Methods for High Dimensional Interactions
 
Atelier r-gerad
Atelier r-geradAtelier r-gerad
Atelier r-gerad
 
Analysis of DNA methylation and Gene expression to predict childhood obesity
Analysis of DNA methylation and Gene expression to predict childhood obesityAnalysis of DNA methylation and Gene expression to predict childhood obesity
Analysis of DNA methylation and Gene expression to predict childhood obesity
 
Estimation and Accuracy after Model Selection
Estimation and Accuracy after Model SelectionEstimation and Accuracy after Model Selection
Estimation and Accuracy after Model Selection
 
Absolute risk estimation in a case cohort study of prostate cancer
Absolute risk estimation in a case cohort study of prostate cancerAbsolute risk estimation in a case cohort study of prostate cancer
Absolute risk estimation in a case cohort study of prostate cancer
 
Computational methods for case-cohort studies
Computational methods for case-cohort studiesComputational methods for case-cohort studies
Computational methods for case-cohort studies
 
Factors influencing participation in cancer screening
Factors influencing participation in cancer screeningFactors influencing participation in cancer screening
Factors influencing participation in cancer screening
 
Introduction to LaTeX
Introduction to LaTeXIntroduction to LaTeX
Introduction to LaTeX
 
Methylation and Expression data integration
Methylation and Expression data integrationMethylation and Expression data integration
Methylation and Expression data integration
 
Reproducible Research
Reproducible ResearchReproducible Research
Reproducible Research
 

Recently uploaded

LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.
Silpa
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
Silpa
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
Silpa
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
NazaninKarimi6
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
levieagacer
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Sérgio Sacani
 
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptxTHE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
ANSARKHAN96
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
Silpa
 

Recently uploaded (20)

LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptx
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
 
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and SpectrometryFAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
FAIRSpectra - Enabling the FAIRification of Spectroscopy and Spectrometry
 
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIACURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
CURRENT SCENARIO OF POULTRY PRODUCTION IN INDIA
 
300003-World Science Day For Peace And Development.pptx
300003-World Science Day For Peace And Development.pptx300003-World Science Day For Peace And Development.pptx
300003-World Science Day For Peace And Development.pptx
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
 
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort ServiceCall Girls Ahmedabad +917728919243 call me Independent Escort Service
Call Girls Ahmedabad +917728919243 call me Independent Escort Service
 
Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.Selaginella: features, morphology ,anatomy and reproduction.
Selaginella: features, morphology ,anatomy and reproduction.
 
Atp synthase , Atp synthase complex 1 to 4.
Atp synthase , Atp synthase complex 1 to 4.Atp synthase , Atp synthase complex 1 to 4.
Atp synthase , Atp synthase complex 1 to 4.
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
 
Clean In Place(CIP).pptx .
Clean In Place(CIP).pptx                 .Clean In Place(CIP).pptx                 .
Clean In Place(CIP).pptx .
 
GBSN - Biochemistry (Unit 2)
GBSN - Biochemistry (Unit 2)GBSN - Biochemistry (Unit 2)
GBSN - Biochemistry (Unit 2)
 
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRingsTransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
TransientOffsetin14CAftertheCarringtonEventRecordedbyPolarTreeRings
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
 
PSYCHOSOCIAL NEEDS. in nursing II sem pptx
PSYCHOSOCIAL NEEDS. in nursing II sem pptxPSYCHOSOCIAL NEEDS. in nursing II sem pptx
PSYCHOSOCIAL NEEDS. in nursing II sem pptx
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
Site Acceptance Test .
Site Acceptance Test                    .Site Acceptance Test                    .
Site Acceptance Test .
 
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptxTHE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
THE ROLE OF BIOTECHNOLOGY IN THE ECONOMIC UPLIFT.pptx
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
 

Reproducible Research: An Introduction to knitr

  • 1. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Reproducible Research An Introduction to knitr Sahir Rai Bhatnagar1 May 28, 2014 1https://github.com/sahirbhatnagar/knitr-tutorial 1 / 38
  • 2. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Acknowledgements • Dr. Erica Moodie • Maxime Turgeon (Windows) • Kevin McGregor (Mac) • Greg Voisin • Don Knuth (TEX) • Friedrich Leisch (Sweave) • Yihui Xie (knitr) • You 2 / 38
  • 3. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Disclaimer #1 • Feel free to Ask questions • Interrupt me often • You don’t need to raise your hand to speak 3 / 38
  • 4. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Disclaimer #2 I don’t work for, nor am I an author of any of these packages. I’m just a messenger. 4 / 38
  • 5. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Disclaimer #3 • 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 5 / 38
  • 6. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Eat Your Own Dog Food • These slides are reproducible • Source code: https://github.com/sahirbhatnagar/knitr- tutorial/tree/master/slides 6 / 38
  • 7. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Main objective for today 7 / 38
  • 8. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks What is Science Anyway? 8 / 38
  • 9. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks What is Science Anyway? According to the American Physical Society: Science is the systematic enterprise of gathering knowledge about the universe and organizing and condensing that knowledge into testable laws and theories. The success and credibility of science are anchored in the willingness of scientists to expose their ideas and results to independent testing and replication by other scientists 8 / 38
  • 10. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks RR: A Minimum Standard to Verify Scientific Findings 9 / 38
  • 11. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks RR: A Minimum Standard to Verify Scientific Findings Reproducible Research (RR) in Computational Sciences The data and the code used to make a finding are available and they are sufficient for an independent researcher to recreate the finding 9 / 38
  • 12. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Why should we care about RR? For Science Standard to judge scientific claims Avoid duplication Cumulative knowledge development
  • 13. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks 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 / 38
  • 14. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks A Motivating Example Demonstrate: 001-motivating-example Survey: https://www.surveymonkey.com/s/CDVXW3C 11 / 38
  • 15. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Tools for Reproducible Research2 Free and Open Source Software • RStudio: Creating, managing, compiling documents • LATEX: Markup language for typesetting a document • R: Statistical analysis language • knitr: Integrate LATEXand R code. Based on Prof. Friedrich Leisch’s Sweave 2http://onepager.togaware.com/ 12 / 38
  • 16. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Comparison Figure 1 : Comparison • LATEX has a greater learning curve • Many tasks are very tedious or impossible (most cases) to do in MS Word or Libre Office 13 / 38
  • 17. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks The Philosophy behind LATEX Figure 2 : Adam Smith, author of The Wealth of Nations (1776), in which he conceptualizes the notion of the division of labour Division of Labour Composition and logical structuring of text is the author’s specific contribution to the production of a printed text. Matters such as the choice of the font family, should section headings be in bold face or small capitals? Should they be flush left or centered? Should the text be justified or not? Should the notes appear at the foot of the page or at the end? Should the text be set in one column or two? and so on, is the typesetter’s business 14 / 38
  • 18. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks The Genius Behind LATEX Figure 3 : The TEX project was started in 1978 by Donald Knuth (Stanford). He planned for 6 months, but it took him nearly 10 years to complete. Coined the term “Literate programming”: mixture of code and text segments that are “human” readable. Recipient of the Turing Award (1974) and the Kyoto Prize (1996). 15 / 38
  • 19. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Integrated Development Environment (IDE) 16 / 38
  • 20. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Integrated Development Environment (IDE) Demonstrate: Explore RStudio 16 / 38
  • 21. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks What knitr does LATEX example: Report.Rnw (contains both code and markup) Report.tex knitr::knit(’Report.Rnw’)
  • 22. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks What knitr does LATEX example: Report.Rnw (contains both code and markup) Report.tex knitr::knit(’Report.Rnw’) Report.pdf latex2pdf(’Report.tex’) 17 / 38
  • 23. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Compiling a .Rnw document The two steps on previous slide can be executed in one command: knitr::knit2pdf() or in RStudio: 18 / 38
  • 24. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Incorporating R code • Insert R code in a Code Chunk starting with << >>= and ending with @ In RStudio: 19 / 38
  • 25. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Example 1 <<example-code-chunk-name, echo=TRUE>>= library(magrittr) rnorm(50) %>% mean @ produces library(magrittr) rnorm(50) %>% mean ## [1] 0.031 20 / 38
  • 26. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Example 2 <<example-code-chunk-name2, echo=TRUE, tidy=TRUE>>= for(i in 1:5){ (i+3) %>% print} @ produces for (i in 1:5) { (i + 3) %>% print } ## [1] 4 ## [1] 5 ## [1] 6 ## [1] 7 ## [1] 8 21 / 38
  • 27. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Example 2.2 <<example-code-chunk-name3, echo=FALSE>>= for(i in 1:5){ (i+3) %>% print} @ produces ## [1] 4 ## [1] 5 ## [1] 6 ## [1] 7 ## [1] 8 22 / 38
  • 28. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Example 2.3 <<example-code-chunk-name4, echo=FALSE, eval=FALSE>>= for(i in 1:5){ (i+3) %>% print} @ produces Demonstrate: Try it yourself 23 / 38
  • 29. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks 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 24 / 38
  • 30. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Include a Figure <<fig.ex, fig.cap='Linear Regression',fig.height=3,fig.width=3>>= plot(mtcars[ , c('disp','mpg')]) lm(mpg ~ disp , data = mtcars) %>% abline(lwd=2) @ 100 200 300 400 1025 disp mpg Figure 4 : Linear regression 25 / 38
  • 31. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Include a Table <<table.ex, results='asis'>>= library(xtable) iris[1:5,1:5] %>% xtable(caption='Sample of Iris data') %>% print(include.rownames=FALSE) @ 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 26 / 38
  • 32. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Minimum Working Example https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/002- minimum-working-example 27 / 38
  • 33. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Extracting output from Regression Models https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/003- model-output 28 / 38
  • 34. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Figures https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/004- figures 29 / 38
  • 35. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Beamer Presentations https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/005- beamer-presentation 30 / 38
  • 36. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Changing one Parameter in an Analysis https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/006- sensitivity-analysis-one-parameter 31 / 38
  • 37. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Changing Many Parameters in an Analysis https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/007- sensitivity-analysis-many-parameters 32 / 38
  • 38. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Large Documents https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/008- large-documents 33 / 38
  • 39. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks HTML Reports https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/009- rmarkdown 34 / 38
  • 40. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks HTML Presentations https://github.com/sahirbhatnagar/knitr-tutorial/tree/master/010- rmarkdown-presentation 35 / 38
  • 41. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks 36 / 38
  • 42. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Always Remember ... Reproducibility ∝ 1 copy paste 37 / 38
  • 43. RR: Intro to knitr Reproducible Research What? Why? 001-motivating- example Getting Started LATEX RStudio knitr Examples 002-minimum- working-example 003-model- output 004-figures 005-beamer- presentation 006-sensitivity- analysis-one- parameter 007-sensitivity- analysis-many- parameters 008-large- documents 009-rmarkdown 010-rmarkdown- presentation Final Remarks Is the juice worth the squeeze? 38 / 38