The Agenda and Learning Goals
Agenda
- What is robust and reproducible research?
- How can you make your research more robust and reproducible?
- You can use RMarkdown! Wait, what’s RMarkdown?
- How to use RMarkdown and why it may change your workflow.
- Using git through RStudio.
- Initialize your a more reproducible github repo within RStudio!
Learning Goals
- Walk away knowing what robust and reproducible research are.
- To know the changes you can make to your research to make it more robust and reproducible.
- To understand what RMarkdown is.
- To know how to create an RMarkdown document and understand it’s uses for code sharing.
- To understand how to use git through the RStudio interface.
- Know how to initialize a more reproducible github repo within RStudio!
Key Resources Used to Build this lesson
We scientists have a few problems
- The LaCour Scandal of fabricated data published in Science.
- Scientists in the United States spend $28 billion each year on basic biomedical research that cannot be repeated successfully
- A reproducibility study in psychology found that only 39 of 100 studies could be reproduced.
- A quote: “A lot of working scientists assume that if it’s published, it’s right… This makes it hard to dismiss that there are still a lot of false positives in the literature.”
- Recent Atlantic Article by Ed Yong
- The Journal Nature on the issue of reproducibility
- A comment from the journal Nature: “Nature and the Nature research journals will introduce editorial measures to address the problem by improving the consistency and quality of reporting in life-sciences articles… we will give more space to methods sections. We will examine statistics more closely and encourage authors to be transparent, for example by including their raw data.”
- Nature also released a checklist with an albeit wimpy computational check (see #18).
- This talk will hopefully help all of us improve this step.
Some Recommendations
5 Recommendations for Robust Research
1a. Write code for humans
Code readability is very important.
- Code should be broken down into small chunks that may be re-used.
- Do not re-write code to do the same task over and over again.
- Do not repeat yourself! (Who wants to read that?)
- If you need to, make it a tool/function.
- Do not require readers to have to think of multiple facts at once.
- Make names/variables consistent, distinctive and meaningful.
- Adopt a style and format and keep it consistent.
- Be a concise and clear commenter.
If your code is more readable, then:
- Your project is more reproducible.
- It’s easier to find and correct bugs.
- You will be your friend in the future when you revisit the code.
1b. Write data for computers
Let your computer do the work for you
Format your data so its easily read by your computer, not by you or other humans.
- Code written for people to read requires cleaning and tidying to be processed by a computer.
- Name data files in a consistent way.
- Automating tasks will be easier, which will prevent you from making trivial mistakes.
2. Make incremental changes.
- Work in small steps with frequent feedback.
- Have a friend or labmate test your code and try to break it.
- Challenge your PI to test your code!
- Use version control!
- Put all manual changes under version control, too!
3. Be a “Defensive Programmer” - Make Assertions
Add tests within your code to make sure your code is doing what it is supposed to do.
Assertions are statments that something holds true. Assertions:
1. Ensure that if something goes wrong, the program will stop.
2. They also explain what the program is doing.
- In R you can use
stopifnot()
- In python you can use
assert()
4. Use existing libraries (packages) whenever possible
- Do not try to re-invent the wheel while your performing your data anaylsis.
- Use functions that have already been written and tested for you.
5. Prevent catastrophe and help reproducibility by making your data read-only
Read-only is important because:
- Modifying data can corrupt your results.
- It’s easy to lose track of how you have changed a file when you modify it in place.
5 Recommendations for Robust Research
- Write code for humans, write data for computers
- Make incremental changes.
- Make assertions and be loud, in code and in your methods
- Use existing libraries (packages) whenever possible
- Prevent catastrophe and help reproducibility by making your data read-only
6 Recommendations for reproducible research
2. Release your code and data
It is simple. Without your code and data, your research is not reproducible.
3. Document everything!
Bottom line: Adopt a computing notebook that is as good as a wet-lab notebook.
To fully reproduce a study, each step of analysis must be described in much more detail than can be included in a publication.
Include a record of your steps, where files are, where they came from, and what they contain.
Include session_info() in your document, preferably at the bottom. Session info lists the version of R that you’re using plus all of the packages you’ve loaded.
In your computing notebook:
- Document your methods and workflows
- Document the origin of all data in your project directory
- Document when and how you downloaded the data
- Record data version info
- Record software version info with
session_info()
For example, all the above information could be stored in a README file
5. Write code that uses relative paths.
Do not rely on hard-coded absolute paths (i.e. /Users/marschmi/Data/seq-data.csv or even ~/Data/seq-data.csv).
Relative paths (i.e. Data/seq-data.csv) or command line arguments are better alternatives.
6. Always Set your seed
If there is any randomizations of data or simulations, use set.seed() in the first code chunk.
Karl Broman suggests to open R and type runif(1, 0, 10^8) and then paste the resulting large number into set.seed() in the first code chunk. If you do this, then the random aspects of your analysis should be repeated the same way.
6 Recommendations for reproducible research
- Encapsulate the full project into one directory that is supported with version control.
- Release your code and data.
- Document everything and use code as documentation!
- Make figures, tables, and statistics the results of scripts.
- Write code that uses relative paths.
- Always Set your seed.
How can you revise your work flow?
- Where you can introduce robust steps?
- Where can you add reproducible steps?
Do you have …
- RStudio?
- R?
- Please install these packages:
install.packages("knitr")
install.packages("rmarkdown")
- A Github account?
- Git?
- If you have not already, please generate an SSH key.
You are good to go!
Using Git through RStudio
- Sign into Github.
- Initialize repo on the github page.
- Name the Repo “NGS_Reproducibility”
- Down in the right-hand corner, copy the SSH clone URL, do not copy the https url!
- Open up RStudio
- File -> New Project -> Version Control -> Git -> Paste the SSH clone URL.
- Be sure to use the same repo name as on your github page!
If you get the following error, you may have copied the HTTPS clone URL instead of the SSH clone url.
Cloning into 'repo_name'...
error: unable to read askpass response from 'rpostback-askpass'
fatal: could not read Username for 'https://github.com': Device not configured
RMarkdown Time!
What is R Markdown?
- RMarkdown is a variant of Markdown that has embedded R code chunks to be used with
knitr to make it easy to create reproducible web-based reports.
- Markdown: A system for writing simple, readable text that is easily converted to html.
- Allows you to write using an easy-to-read, easy-to-write plain text format.
- Rmd -> md -> html (docx, pdf)
- Can include both text and code to execute
Why R Markdown?
A convenient tool for reproducible and dynamic reports with R!
- Execute code with
knitr.
- Easy to learn syntax.
- Include LaTeX equations.
- Don’t need to worry about page breaks or figure placement.
- Consolidate your code and write up into a single file:
- Slideshows, pdfs, html documents, word files
- It’s so easy to use with version control with Git!
Simple Workflow

How to Open an Rmd File


Choose Output
YAML Header: A set of key value pairs at the start of your file. Begin and end the header with a line of three dashes (—)
R Studio template writes the YAML header for you
output: html_document
output: pdf_document
output: word_document
output: beamer_presentation (beamer slideshow - pdf)
output: ioslides_presentation (ioslides presentation - html)
For example: Here’s the YAML header for this webpage with a table of contents.
---
title: "Reproducible Research Using RMarkdown and Git through RStudio"
subtitle: "Tutorial for NGS Workshop Week 3"
author: "Marian L. Schmidt, @micro_marian, marschmi@umich.edu"
date: "August 25, 2015"
output:
html_document:
theme: united
toc: yes
---
Markdown basics
Markdown is a simple formatting language that is easy to use
- Create lists with
* or + sign
- Use one or two asterisk marks to provide emphasis such as
*italics* and **bold**. Can even include tables:
| Content Cell |
Content Cell |
| Content Cell |
Content Cell |
Markdown basics

R Code Chunks
Code blocks display with fixed-width font
#quick summary
library(ggplot2)
min(diamonds$price)
## [1] 326
mean(diamonds$price)
## [1] 3932.8
max(diamonds$price)
## [1] 18823
More R Code Chunks

You can name the code chunk.
echo = TRUE: The code will be displayed.
eval = TRUE: Yes, execute the code.
R Code Chunk Arguments

R Code Chunks: Displaying Plots


Global Chunk Options
You may want to use the same set of chunk options throughout a document and you don’t want to retype those options in every chunk.
Global chunk options are for you!

Inline R Code
You can evaluate expressions inline by enclosing the expression within a single back-tick qualified with r.
Inline code is underappreciated!
Last night, I saw 7 shooting stars!

Rendering document
- Run
rmarkdown::render("<filepath>")
- Click the very cute knit HTML button at the top of the RStudio scripts pane
When you render, R will:
Execute each embedded code chunk and insert the results into your report.
Build a new version of your report in the output file type.
Open a preview of the output file in the viewer pane.
Save the output file in your working directory.
A Git Workflow within RStudio
- Make some changes to your document that you would like to save a copy of.
- Git add by checking the box under “staged” in the git screen.
- Hint: The git screen is in the same pane as the RStudio Environment and history.
- Draft your commit message.
- It should be a meaningful message!
- Think of you in 6 months looking for changes you had made to your document. Don’t you want to be your own friend?
- Do not allow your commit messages to get less informative as your project continues
- Click “Commit.”
- Perform Git Push by clicking the bright green arrow, which is the “git Push” button.
- Make sure everything is pushed to the remote repository without any errors.
Click on the clock button to view your git history. Here, you can also view the difference between documents.
Resources
Resources for Reproducible Research
Resources for Rmarkdown, RStudio and R
About Me
- Twitter: @micro_marian
- e-mail: marschmi@umich.edu
- I’m a native Michigander who likes:
- To code in R
- Sharing Code
- Reproducible science
- PhD Candidate in the Department of Ecology and Evolutionary Biology at the University of Michigan in Vincent Denef’s Lab.
- I recently got involved in the Software Carpentry foundation.
- I’m a part of the U of M FEMMES (Females Excelling More in Math Enginnering and Science) chapter. FEMMES is a group that empowers 4-6th grade girls in STEM fields by holding after school science activities and a once-a-semester capstone “Day in the Lab” at U of M.
- I went to Hampshire College for undergrad and wrote my thesis on the evolution of magnetotaxis in magnetotactic bacteria.
- Next, I worked as a lab tech for a year studying the intracacies of sulfate-reducing bacteria.
- My dissertation is focused on the spatial and temporal distributions and activities of freshwater bacteria.
- Specifically, I work on the bacterial community composition of particle-associated (3-20 um) and free-living (0.22-3 um) bacteria within small inland lakes and in Lake Michigan.
- So far I have been using 16S rRNA amplicon data at the DNA-level. In the future, I hope to look at functional differences between the particle-associated and free-living bacterial fractions by using metatranscriptomics.
- I get to work in this beautiful place…

- When I’m not doing science-y things, you can find me here:

Session Info
Karl Broman recommends using the session_info() from the devtools package.
devtools::session_info()
## setting value
## version R version 3.2.2 (2015-08-14)
## system x86_64, darwin13.4.0
## ui X11
## language (EN)
## collate en_US.UTF-8
## tz America/Detroit
##
## package * version date source
## colorspace 1.2-6 2015-03-11 CRAN (R 3.2.0)
## curl 0.9.3 2015-08-25 CRAN (R 3.2.2)
## devtools 1.8.0 2015-05-09 CRAN (R 3.2.0)
## digest 0.6.8 2014-12-31 CRAN (R 3.2.0)
## evaluate 0.7.2 2015-08-13 CRAN (R 3.2.0)
## formatR 1.2 2015-04-21 CRAN (R 3.2.0)
## ggplot2 * 1.0.1 2015-03-17 CRAN (R 3.2.0)
## git2r 0.11.0 2015-08-12 CRAN (R 3.2.0)
## gtable 0.1.2 2012-12-05 CRAN (R 3.2.0)
## htmltools 0.2.6 2014-09-08 CRAN (R 3.2.0)
## knitr 1.11 2015-08-14 CRAN (R 3.2.2)
## labeling 0.3 2014-08-23 CRAN (R 3.2.0)
## magrittr 1.5 2014-11-22 CRAN (R 3.2.0)
## MASS 7.3-43 2015-07-16 CRAN (R 3.2.2)
## memoise 0.2.1 2014-04-22 CRAN (R 3.2.0)
## munsell 0.4.2 2013-07-11 CRAN (R 3.2.0)
## plyr 1.8.3 2015-06-12 CRAN (R 3.2.0)
## proto 0.3-10 2012-12-22 CRAN (R 3.2.0)
## Rcpp 0.12.0 2015-07-25 CRAN (R 3.2.0)
## reshape2 1.4.1 2014-12-06 CRAN (R 3.2.0)
## rmarkdown 0.7 2015-06-13 CRAN (R 3.2.0)
## rversions 1.0.2 2015-07-13 CRAN (R 3.2.0)
## scales 0.3.0 2015-08-25 CRAN (R 3.2.2)
## stringi 0.5-5 2015-06-29 CRAN (R 3.2.0)
## stringr 1.0.0 2015-04-30 CRAN (R 3.2.0)
## xml2 0.1.1 2015-06-02 CRAN (R 3.2.0)
## yaml 2.1.13 2014-06-12 CRAN (R 3.2.0)