37
Introduction to Reproducible Research in R and R Studio. Susan Johnston April 1, 2016

Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Introduction to Reproducible Researchin R and R Studio.

Susan Johnston

April 1, 2016

mihle
Text Box
today presented by: Malika Ihle
Page 2: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Many of us are clicking, copying and pasting...

I Can you repeat all of this again. . .

I . . . and would you get the same results every time?

Page 3: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Scenarios that benefit from reproducibility

I New raw data becomes available.

I You return to the project after a period of time.

I Project gets handed to new PhD student/postdoc.

I Working collaboratively.

I A reviewer wants you to change a model parameter.

I When you find an error, but not sure where you went wrong.

mihle
Line
Page 4: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Scenarios that benefit from reproducibility

I New raw data becomes available.

I You return to the project after a period of time.

I Project gets handed to new PhD student/postdoc.

I Working collaboratively.

I A reviewer wants you to change a model parameter.

I When you find an error, but not sure where you went wrong.

mihle
Line
Page 5: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Scenarios that benefit from reproducibility

I New raw data becomes available.

I You return to the project after a period of time.

I Project gets handed to new PhD student/postdoc.

I Working collaboratively.

I A reviewer wants you to change a model parameter.

I When you find an error, but not sure where you went wrong.

mihle
Line
Page 6: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Scenarios that benefit from reproducibility

I New raw data becomes available.

I You return to the project after a period of time.

I Project gets handed to new PhD student/postdoc.

I Working collaboratively.

I A reviewer wants you to change a model parameter.

I When you find an error, but not sure where you went wrong.

mihle
Line
mihle
Line
mihle
Line
Page 7: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Four rules for reproducibility.

1. Create a portable project.

2. Avoid manual data manipulation steps - use code!

3. Connect results to text.

4. Version control all custom scripts and documents.

Page 8: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

The Environment: http://www.rstudio.com

Page 9: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Reproducible Research in

1. Creating a Portable Project (.Rproj)

2. Automate analyses - stop clicking and start typing.

3. Dynamic report writing with R Markdown and knitr

4. Version control using git

Page 10: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Reproducible Research in .

1. Creating a Portable Project (.Rproj)

2. Automate analyses - stop clicking and start typing.

3. Dynamic report writing with R Markdown and knitr

4. Version control using git

Page 11: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Structuring an R Project.http://nicercode.github.io/blog/2013-05-17-organising-my-project/

All data, scripts and output should be kept within the same projectdirectory.

Page 12: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Structuring an R Project.http://robjhyndman.com/hyndsight/workflow-in-r/

I load.R - read in data from files

I clean.R - pre-processing and cleaning

I functions.R - define what you need for anlaysis

I do.R - do the analysis!

Page 13: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Structuring an R Project.http://nicercode.github.io/blog/2013-05-17-organising-my-project/

All data, scripts and output should be kept within the same projectdirectory.

Page 14: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Structuring an R Project.http://nicercode.github.io/blog/2013-05-17-organising-my-project/

All data, scripts and output should be kept within the same projectdirectory.

mihle
Text Box
+ README.txt
Page 15: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Bad habits can hinder portability.https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects

setwd("C:/Users/susjoh/Desktop/SalmoAnalysis")

setwd("C:/Users/Susan Johnston/Desktop/SalmoAnalysis")

setwd("C:/Users/sjohns10/Drive/SalmoAnalysis")

source("../../OvisAnalysis/GWASplotfunctions.R")

An analysis should be contained within a directory, and it shouldbe easy to move it or pass on to someone new.

Page 16: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Solution: using Projects.https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects

I Establishes a directory with associated .Rproj file.

I Automatically sets the working directory.

I Can save and source .Rprofile, .Rhistory, .Rdata files.

I Allows version control within R Studio.

Page 17: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Creating a Portable Project (.Rproj)

mihle
Text Box
core workshop 1
Page 18: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Reproducible Package Management

Tools > Project Option > Packrat

Page 19: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Reproducible Research in .

1. Creating a Portable Project (.Rproj)

2. Automate analyses - stop clicking and start typing.

3. Dynamic report writing with R Markdown and knitr

4. Version control using git

Page 20: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

This is R. There is no “if”. Only “how”.

I CRAN, Bioconductor, github

I Reading in data and functions

read.table(), read.csv(), read.xlsx(), source()

I Reorganising data

reshape, plyr, dplyr

I Generate figures

plot(), library(ggplot2)

I Running external programmes with system()

Unix/Mac: system("plink -file OvGen --freq")

Windows: system("cmd", input = "plink -file OvGen --freq")

Page 21: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Reproducible Research in .

1. Creating a Portable Project (.Rproj)

2. Automate analyses - stop clicking and start typing.

3. Dynamic report writing with R Markdown and knitr

4. Version control using git

Page 22: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Output script as Notebook

Page 23: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with
Page 24: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Write reports directly in R

Page 25: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

A Quick Start Guidehttp://nicercode.github.io/guides/reports/

http://rmarkdown.rstudio.com/?version=0.98.1103&mode=desktop

Page 26: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Creating an R Markdown Script (.Rmd).

mihle
Text Box
parallel workshop session 1
Page 27: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Creating an R Markdown Script (.Rmd).

mihle
Text Box
parallel workshop session 1
mihle
Text Box
simultaneous workshop: pre registration walk through
Page 28: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Reproducible Research in .

1. Creating a Portable Project (.Rproj)

2. Automate analyses - stop clicking and start typing.

3. Dynamic report writing with R Markdown and knitr

4. Version control using git

Page 29: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Version Control in R Studio

Tools >Project Options allows setup of git version control.

mihle
Text Box
core workshop 2
Page 30: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Version Control in R Studio

git information will appear in the top-right frame.

Page 31: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Version Control in R Studio

git information will appear in the top-right frame.

Page 32: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Version Control in R Studio

Select files to version control, write a meaningful commit message>Commit

Page 33: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Version Control in R Studio

Previous versions can be viewed and restored from the History tab.

Page 34: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Advanced Steps: Github

I Forking projects

I All scripts are backed up online

I Facilitates collaboration and working on different computers

mihle
Text Box
core workshop 2
Page 35: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Advanced Steps: Github

I Forking projects

I All scripts are backed up online

I Facilitates collaboration and working on different computers

mihle
Text Box
parallel workshop session 2
mihle
Text Box
core workshop 2
Page 36: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Advanced Steps: Github

I Forking projects

I All scripts are backed up online

I Facilitates collaboration and working on different computers

mihle
Text Box
parallel workshop session 2
mihle
Text Box
core workshop 2
mihle
Text Box
simultaneous workshop: considerations when opening dataset
Page 37: Introduction to Reproducible Research in R and R Studio. · Creating a Portable Project (.Rproj) 2. Automate analyses - stop clicking and start typing. 3. Dynamic report writing with

Take home messages

I Manage projects reproducibly: The first researcher who willneed to reproduce the results is likely to be YOU.

I Time invested in learning to code pays off - do it.

I Supervisors should be patient and encourage students to code.