SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Introduction

Details

Reproducible research with RStudio and knitr
Ben Bolker
McMaster University, Mathematics & Statistics and Biology

25 February 2014

Ben Bolker
RStudio and knitr
Introduction

Details

Reproducible research

Science requires reproducibility
Computational methods: harder to reproduce than
math/analytic methods
(but easier than non-computational science)
Maintain integrity
Disseminate knowledge
Maintain personal sanity

Ben Bolker
RStudio and knitr
Introduction

Details

Literate programming

Knuth (1984)
ancestor of RR
similar tools (WEB/weave/tangle), but different scope
targets code as a document with interwoven discussion
some notes on the LP-RR ecosystem

Ben Bolker
RStudio and knitr
Introduction

Details

A
TEX/LTEX

Knuth (1978)/Lamport (1980s)
mathematical (and general-purpose) typesetting system
pro: beautiful, widely used, cross-platform, customizable,
stable
con: old-fashioned, arcane

Ben Bolker
RStudio and knitr
Introduction

Details

R

Gentleman and Ihaka, 1990s
statistical programming language/data analysis environment
pro: powerful, widely used (3000+ packages), cross-platform,
customizable
con: slower than low-level languages; organic/inconsistent

Ben Bolker
RStudio and knitr
Introduction

Details

Sweave/knitr

Leisch / Xie
A
literate programming tool, allowing LTEX chunks in R
highlighted code chunks (echo=TRUE)

automatically generated figures, optionally in a figure
environment
pro: super-convenient, once you get used to it
con: one more software layer;
not suited for big projects/code

Ben Bolker
RStudio and knitr
Introduction

Details

RStudio

Allaire et al.
front-end for R
one-button front end for knitr (“Compile PDF”)
pro: beginner-friendly; cross-platform;
zoomable graphics, code highlighting, tab completion,
environment listing, etc.
con: R-centric; restriction to built-in editor;
one more software layer

Ben Bolker
RStudio and knitr
Introduction

Details

Getting started
bookmark the knitr web page and especially the options page
switch RStudio to compile documents with knitr
(Tools/Global options/Sweave/Weave Rnw files using ...)
A
make sure LTEX is installed/working and the knitr package
is installed (install.packages("knitr")); also install
tikzDevice package

build this document, or use (File/New File/R Sweave) to
generate an empty template (need to add something to it);
RStudio recognizes .Rnw extension
code chunks start with <<>>= and end with @

Ben Bolker
RStudio and knitr
Introduction

Details

Troubleshooting

load the knitr package in the R console: library("knitr")
R code failing? Try running it interactively in the console, or
purl() to pull the code into a separate file
in the console:
knit2pdf("myfile.Rnw") = pushing the button
step by step: knit("myfile.Rnw") +
externally pdflatex myfile
always name your code chunks!
RStudio always compiles PDF in a clean environment

Ben Bolker
RStudio and knitr
Introduction

Details

Code options
Set per chunk, e.g. <<mychunk,echo=TRUE,eval=FALSE>>
or globally via opts chunk$set(...)
eval: evaluate?
echo: show code?
warning/message/error: show/stop? (knitr does not stop
on errors by default!)
results: "markup" is default, I sometimes use "hide"
tidy: reformat code?
cache: cache results?

Ben Bolker
RStudio and knitr
Introduction

Details

More stuff about code

if you’re using beamer, need to use
begin{frame}[fragile] if you’re going to show code (i.e.,
echo=TRUE)
code in chunks must be complete/syntactically correct: no
fragments allowed;
can’t (e.g.) separate parts of a for loop, even if eval=FALSE
in-line expressions via Sexpr{} (don’t forget to round
numeric values)

Ben Bolker
RStudio and knitr
Introduction

Code example

library(nlme)
## comments get formatted nicely too
fm1 <- lme(distance ~ age, data = Orthodont)

Ben Bolker
RStudio and knitr

Details
Introduction

Details

Graphics
Graphics automatically run (stored in figures directory)
fig.width, fig.height control the size/aspect ratio of the
plot window (in inches!)
out.width controls the size of the printed plot
A
(in LTEX units, e.g. "0.7textwidth")
(note double backslashes)
dev controls device: default is ”pdf”, may want ”png” for
A
huge figures or ”tikz” for LTEX fonts and symbols (not for big
figures!)
fig.cap generates a figure caption and puts the plot in a
figure environment (need math mode where appropriate,
and double backslashes!)
Ben Bolker
RStudio and knitr
Introduction

Details

0
−2

−1

rnorm(100)

1

2

Graphics example: basic

−2

Ben Bolker
RStudio and knitr

−1

0
rnorm(100)

1

2
Introduction

Details

2
0
−2

rnorm(100)

Graphics example: fig.width=3,fig.height=3

−3

−1 0

1

rnorm(100)
Ben Bolker
RStudio and knitr

2
Introduction

Details

Graphics example: dev="png"

0
−2

rnorm(10000)

2

4

plot(rnorm(10000), rnorm(10000))

−4

−2

0
rnorm(10000)

Ben Bolker
RStudio and knitr

2

4
Introduction

Details

Graphics example: dev="tikz"

0
-2

-1

√

ξα

1

2

plot(rnorm(100),rnorm(100),
xlab="${cal R}_0$",ylab="$sqrt{xi^alpha}$")

-3

-2

-1

0
R0

Ben Bolker
RStudio and knitr

1

2

3
Introduction

Details

Other stuff

other input formats: R markdown
chunks in other languages
output to other formats: HTML, docx
other ways of documenting/disseminating results:
commented R code (spin()); R packages/vignettes;
roxygen2 package
large/batch jobs: caching gets tricky, use Makefiles instead?
figure tricks: 3D (rgl) plots, animation . . .

Ben Bolker
RStudio and knitr
Introduction

Details

Further resources

knitr web page, including the demos and showcase . . .
StackOverflow
my examples on Rpubs
knitr book on Amazon

Ben Bolker
RStudio and knitr

Contenu connexe

En vedette

Davis eco-evo virulence
Davis eco-evo virulenceDavis eco-evo virulence
Davis eco-evo virulence
Ben Bolker
 
Stats sem 2013
Stats sem 2013Stats sem 2013
Stats sem 2013
Ben Bolker
 
virulence evolution (IGERT symposium)
virulence evolution (IGERT symposium)virulence evolution (IGERT symposium)
virulence evolution (IGERT symposium)
Ben Bolker
 
computational science & engineering seminar, 16 oct 2013
computational science & engineering seminar, 16 oct 2013computational science & engineering seminar, 16 oct 2013
computational science & engineering seminar, 16 oct 2013
Ben Bolker
 

En vedette (20)

Presentation1
Presentation1Presentation1
Presentation1
 
10 Mistakes to avoid in data science
10 Mistakes to avoid in data science10 Mistakes to avoid in data science
10 Mistakes to avoid in data science
 
Davis eco-evo virulence
Davis eco-evo virulenceDavis eco-evo virulence
Davis eco-evo virulence
 
Stats sem 2013
Stats sem 2013Stats sem 2013
Stats sem 2013
 
evolution of virulence: devil in the details
evolution of virulence: devil in the detailsevolution of virulence: devil in the details
evolution of virulence: devil in the details
 
Igert glmm
Igert glmmIgert glmm
Igert glmm
 
Waterloo GLMM talk
Waterloo GLMM talkWaterloo GLMM talk
Waterloo GLMM talk
 
Bolker esa2014
Bolker esa2014Bolker esa2014
Bolker esa2014
 
Threads 2013
Threads 2013Threads 2013
Threads 2013
 
Google lme4
Google lme4Google lme4
Google lme4
 
model complexity and model choice for animal movement models
model complexity and model choice for animal movement modelsmodel complexity and model choice for animal movement models
model complexity and model choice for animal movement models
 
Waterloo GLMM talk
Waterloo GLMM talkWaterloo GLMM talk
Waterloo GLMM talk
 
math bio for 1st year math students
math bio for 1st year math studentsmath bio for 1st year math students
math bio for 1st year math students
 
virulence evolution (IGERT symposium)
virulence evolution (IGERT symposium)virulence evolution (IGERT symposium)
virulence evolution (IGERT symposium)
 
computational science & engineering seminar, 16 oct 2013
computational science & engineering seminar, 16 oct 2013computational science & engineering seminar, 16 oct 2013
computational science & engineering seminar, 16 oct 2013
 
Threads 2013
Threads 2013Threads 2013
Threads 2013
 
ESS of minimal mutation rate in an evo-epidemiological model
ESS of minimal mutation rate in an evo-epidemiological modelESS of minimal mutation rate in an evo-epidemiological model
ESS of minimal mutation rate in an evo-epidemiological model
 
A Shiny Example-- R
A Shiny Example-- RA Shiny Example-- R
A Shiny Example-- R
 
Montpellier
MontpellierMontpellier
Montpellier
 
Introduction to knitr - May Sheffield R Users group
Introduction to knitr - May Sheffield R Users groupIntroduction to knitr - May Sheffield R Users group
Introduction to knitr - May Sheffield R Users group
 

Similaire à intro to knitr with RStudio

Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
anshikagoel52
 

Similaire à intro to knitr with RStudio (20)

Language-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible researchLanguage-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible research
 
Chap1
Chap1Chap1
Chap1
 
Python Course Basic
Python Course BasicPython Course Basic
Python Course Basic
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbai
 
Raspberry pi glossary of terms dictionary extended
Raspberry pi glossary of terms dictionary extendedRaspberry pi glossary of terms dictionary extended
Raspberry pi glossary of terms dictionary extended
 
Data analysis in R
Data analysis in RData analysis in R
Data analysis in R
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
slides1-introduction to python-programming.pptx
slides1-introduction to python-programming.pptxslides1-introduction to python-programming.pptx
slides1-introduction to python-programming.pptx
 
SDAccel Design Contest: Xilinx SDAccel
SDAccel Design Contest: Xilinx SDAccel SDAccel Design Contest: Xilinx SDAccel
SDAccel Design Contest: Xilinx SDAccel
 
Docopt, beautiful command-line options for R, user2014
Docopt, beautiful command-line options for R,  user2014Docopt, beautiful command-line options for R,  user2014
Docopt, beautiful command-line options for R, user2014
 
AQA Computer science easter revision
AQA Computer science easter revisionAQA Computer science easter revision
AQA Computer science easter revision
 
Socket programming-in-python
Socket programming-in-pythonSocket programming-in-python
Socket programming-in-python
 
Getting Started with Hadoop
Getting Started with HadoopGetting Started with Hadoop
Getting Started with Hadoop
 
Introduction to F#
Introduction to F#Introduction to F#
Introduction to F#
 
BeagleBone Black: Platform Bring-Up with Upstream Components
BeagleBone Black: Platform Bring-Up with Upstream ComponentsBeagleBone Black: Platform Bring-Up with Upstream Components
BeagleBone Black: Platform Bring-Up with Upstream Components
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
Lecture1_R.pdf
Lecture1_R.pdfLecture1_R.pdf
Lecture1_R.pdf
 

Plus de Ben Bolker

MBRS detectability talk
MBRS detectability talkMBRS detectability talk
MBRS detectability talk
Ben Bolker
 
MBI intro to spatial models
MBI intro to spatial modelsMBI intro to spatial models
MBI intro to spatial models
Ben Bolker
 
Harvard Forest GLMM talk
Harvard Forest GLMM talkHarvard Forest GLMM talk
Harvard Forest GLMM talk
Ben Bolker
 
ESA 2011 pines/spatial talk
ESA 2011 pines/spatial talkESA 2011 pines/spatial talk
ESA 2011 pines/spatial talk
Ben Bolker
 
unmarked individuals: Guelph
unmarked individuals: Guelphunmarked individuals: Guelph
unmarked individuals: Guelph
Ben Bolker
 

Plus de Ben Bolker (14)

Ecological synthesis across scales: West Nile virus in individuals and commun...
Ecological synthesis across scales: West Nile virus in individuals and commun...Ecological synthesis across scales: West Nile virus in individuals and commun...
Ecological synthesis across scales: West Nile virus in individuals and commun...
 
model complexity and model choice for animal movement models
model complexity and model choice for animal movement modelsmodel complexity and model choice for animal movement models
model complexity and model choice for animal movement models
 
Fundamental principles (?) of biological data
Fundamental principles (?) of biological dataFundamental principles (?) of biological data
Fundamental principles (?) of biological data
 
MBRS detectability talk
MBRS detectability talkMBRS detectability talk
MBRS detectability talk
 
Disease-induced extinction
Disease-induced extinctionDisease-induced extinction
Disease-induced extinction
 
Zif bolker_w2
Zif bolker_w2Zif bolker_w2
Zif bolker_w2
 
Trondheim glmm
Trondheim glmmTrondheim glmm
Trondheim glmm
 
MBI intro to spatial models
MBI intro to spatial modelsMBI intro to spatial models
MBI intro to spatial models
 
Harvard Forest GLMM talk
Harvard Forest GLMM talkHarvard Forest GLMM talk
Harvard Forest GLMM talk
 
ESA 2011 pines/spatial talk
ESA 2011 pines/spatial talkESA 2011 pines/spatial talk
ESA 2011 pines/spatial talk
 
Open source GLMM tools: Concordia
Open source GLMM tools: ConcordiaOpen source GLMM tools: Concordia
Open source GLMM tools: Concordia
 
open-source GLMM tools
open-source GLMM toolsopen-source GLMM tools
open-source GLMM tools
 
unmarked individuals: Guelph
unmarked individuals: Guelphunmarked individuals: Guelph
unmarked individuals: Guelph
 
GLMs and extensions in R
GLMs and extensions in RGLMs and extensions in R
GLMs and extensions in R
 

Dernier

Dernier (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 

intro to knitr with RStudio