Skip to content

Latest commit

 

History

History
1360 lines (884 loc) · 78.2 KB

RMarkdown_Lesson.md

File metadata and controls

1360 lines (884 loc) · 78.2 KB

Creating Dynamic Documents with RMarkdown and Knitr

By: Marian L. Schmidt, @micro_marian, marschmi at umich.edu
May 11th, 2016


Welcome!

This workshop was hosted on May 11th, 2016 and has 2 supplemental materials available:

  1. Class notes can be found on the workshop etherpad.
  2. This workshop was recorded live and is available on YouTube. Please be welcome to tune in on YouTube!

This tutorial was constructed as a part of Dr. C Titus Brown's Data Intensive Biology (DIB) training program at the University of California, Davis. The DIB training program hosts local + remote workshops covering topics in bioinformatic tools and data analysis.

There were three remote classrooms that tuned in:

  1. University of California, Davis
  2. Simon Fraser University
  3. Ontario Institute for Cancer Research

The Github repository for this lesson can be found here.

Installation Instructions

Before working through the workshop materials, please do the following in preparation:

  1. Open up RStudio.
  2. Install and download the devtools R package by running the following in the R console.
install.packages("devtools") # To include the session information
library("devtools") # To include the session information  
  1. Check that you have the correct versions of R and RStudio by running devtools::session_info() in the R console.
    Here, devtools:: tells R to use the session_info() function within the devtools package rather than the sessionInfo() function within the utils package. Running devtools::session_info() allows us to see the version of R and RStudio.

Do you have the following versions of R and RStudio?

  • R: Version 3.3.0 (2016-05-03)

  • RStudio: 0.99.1172

    • If you do then you are good to go!
    • If you do not have the correct versions of R and RStudio, please follow the Setup directions on our course page.
  1. Install other R packages necessary for the workshop.
## Install the correct packages
install.packages("rmarkdown") # Make cool dynamic documents
install.packages("knitr") # Run R Code Chunks
install.packages("ggplot2") # For plotting
install.packages("DT") # Interactive HTML tables
## Add these packages to the current session to make sure they load correctly
library("rmarkdown") # Make cool dynamic documents
library("knitr") # Run R Code Chunks
library("ggplot2") # For plotting
library("DT") # Interactive HTML tables
  1. If your packages loaded without any errors, then you are ready for the workshop!

If there were issues with the above packages please place a pink sticky to get help from a helper!


Amazing Resources

I could not have made this tutorial without these amazing resources:

  1. The RMarkdown website hosted by RStudio.
  2. Dr. Yuhui Xie's book: Dynamic Documents with R and Knitr 2^nd^ Edition [@Xie2015] and his Knitr website.
    • A BIG thank you to Dr. Xie for writing the Knitr Package!!
  3. Dr. Karl Broman's "Knitr in a Knutshell".
  4. Cheatsheets released by RStudio.

Dynamic Documents

Literate programming is the basic idea behind dynamic documents and was proposed by Donald Knuth in 1984. Originally, it was for mixing the source code and documentation of software development together. Today, we will create dynamic documents in which program or analysis code is run to produce output (e.g. tables, plots, models, etc) and then are explained through narrative writing.

The 3 steps of Literate Programming:

  1. Parse the source document and separate code from narratives.
  2. Execute source code and return results.
  3. Mix results from the source code with the original narratives.

So that leaves 2 steps for us which includes writing:

  1. Analysis code
  2. A narrative to explain the results from the analysis code.

Traditionally, people used comments to include narrative in their code file (for R that would be a .R file). For example, the file may include the following:

# Title:  Relationship of Car Weight and Gas Efficiency 
# By:  Marian Schmidt  
# Date:  May 11th, 2016

# I predict that there will be a relationship between weight and miles per gallon.  
# I will test this by running a linear model with the mtcars dataset is within the R datasets

# What does the data look like?
datatable(mtcars) # Interactive table 

# Is there a relationship between the weight of a car and the miles per gallon?
lm_mpg <- lm(mpg ~ wt, data = mtcars) # Run linear model predicting mpg based on wt
coef_lm_mpg <- coef(summary(lm_mpg)) # Extract coefficients to table 
kable(coef_lm_mpg) # Produce non-interactive table - function in knitr

# Plot the relationship between weight and miles per gallon  
plot <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + # 
  geom_smooth(method = "lm") + theme_bw() + # add linear model and make black and white
  xlab("Weight (1000lbs)") + ylab("Miles per Gallon") # Add axis labels
ggplotly(plot) # Make the plot interactive  

# It appears that with a gain in every 1000 pounds that there will be a decrease in fuel efficiency by 5.34 miles per gallon
# The end

Then the user would have to read the comments and run the code themselves.

However, literate programming allows us to run the code and describe the code all within one document that we could share. So for example we could do the following:


Relationship of Car Weight and Gas Efficiency
By: Marian Schmidt
Date: May 11th, 2016

I predict that there will be a relationship between weight and miles per gallon.

I will test this by running a linear model with the mtcars dataset is within the R data sets.

# What does the data look like?
datatable(mtcars) # Interactive table 
<script type="application/json" data-for="htmlwidget-3807">{"x":{"data":[["Mazda RX4","Mazda RX4 Wag","Datsun 710","Hornet 4 Drive","Hornet Sportabout","Valiant","Duster 360","Merc 240D","Merc 230","Merc 280","Merc 280C","Merc 450SE","Merc 450SL","Merc 450SLC","Cadillac Fleetwood","Lincoln Continental","Chrysler Imperial","Fiat 128","Honda Civic","Toyota Corolla","Toyota Corona","Dodge Challenger","AMC Javelin","Camaro Z28","Pontiac Firebird","Fiat X1-9","Porsche 914-2","Lotus Europa","Ford Pantera L","Ferrari Dino","Maserati Bora","Volvo 142E"],[21,21,22.8,21.4,18.7,18.1,14.3,24.4,22.8,19.2,17.8,16.4,17.3,15.2,10.4,10.4,14.7,32.4,30.4,33.9,21.5,15.5,15.2,13.3,19.2,27.3,26,30.4,15.8,19.7,15,21.4],[6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4],[160,160,108,258,360,225,360,146.7,140.8,167.6,167.6,275.8,275.8,275.8,472,460,440,78.7,75.7,71.1,120.1,318,304,350,400,79,120.3,95.1,351,145,301,121],[110,110,93,110,175,105,245,62,95,123,123,180,180,180,205,215,230,66,52,65,97,150,150,245,175,66,91,113,264,175,335,109],[3.9,3.9,3.85,3.08,3.15,2.76,3.21,3.69,3.92,3.92,3.92,3.07,3.07,3.07,2.93,3,3.23,4.08,4.93,4.22,3.7,2.76,3.15,3.73,3.08,4.08,4.43,3.77,4.22,3.62,3.54,4.11],[2.62,2.875,2.32,3.215,3.44,3.46,3.57,3.19,3.15,3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.2,1.615,1.835,2.465,3.52,3.435,3.84,3.845,1.935,2.14,1.513,3.17,2.77,3.57,2.78],[16.46,17.02,18.61,19.44,17.02,20.22,15.84,20,22.9,18.3,18.9,17.4,17.6,18,17.98,17.82,17.42,19.47,18.52,19.9,20.01,16.87,17.3,15.41,17.05,18.9,16.7,16.9,14.5,15.5,14.6,18.6],[0,0,1,1,0,1,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,1],[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1],[4,4,4,3,3,3,3,4,4,4,4,3,3,3,3,3,3,4,4,4,3,3,3,3,3,4,5,5,5,5,5,4],[4,4,1,1,2,1,4,2,2,4,4,3,3,3,4,4,4,1,2,1,1,2,2,4,2,1,2,2,4,6,8,2]],"container":"\n \n \n
\u003c/th>\n mpg\u003c/th>\n cyl\u003c/th>\n disp\u003c/th>\n hp\u003c/th>\n drat\u003c/th>\n wt\u003c/th>\n qsec\u003c/th>\n vs\u003c/th>\n am\u003c/th>\n gear\u003c/th>\n carb\u003c/th>\n \u003c/tr>\n \u003c/thead>\n\u003c/table>","options":{"columnDefs":[{"className":"dt-right","targets":[1,2,3,4,5,6,7,8,9,10,11]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false},"callback":null,"filter":"none"},"evals":[],"jsHooks":[]}</script>
# Is there a relationship between the weight of a car and the miles per gallon?
lm_mpg <- lm(mpg ~ wt, data = mtcars) # Run linear model predicting mpg based on wt
coef_lm_mpg <- coef(summary(lm_mpg)) # Extract coefficients to table 
kable(coef_lm_mpg) # Produce non-interactive table - function in knitr
            Estimate   Std. Error     t value   Pr(>|t|)

(Intercept) 37.285126 1.877627 19.857575 0 wt -5.344472 0.559101 -9.559044 0

# Plot the relationship between weight and miles per gallon  
plot <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + # 
  geom_smooth(method = "lm") + theme_bw() + # add linear model and make black and white
  xlab("Weight (1000lbs)") + ylab("Miles per Gallon") # Add axis labels
ggplotly(plot) # Make the plot interactive
<script type="application/json" data-for="htmlwidget-3721">{"x":{"data":[{"x":[2.62,2.875,2.32,3.215,3.44,3.46,3.57,3.19,3.15,3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.2,1.615,1.835,2.465,3.52,3.435,3.84,3.845,1.935,2.14,1.513,3.17,2.77,3.57,2.78],"y":[21,21,22.8,21.4,18.7,18.1,14.3,24.4,22.8,19.2,17.8,16.4,17.3,15.2,10.4,10.4,14.7,32.4,30.4,33.9,21.5,15.5,15.2,13.3,19.2,27.3,26,30.4,15.8,19.7,15,21.4],"text":["wt: 2.62
mpg: 21","wt: 2.88
mpg: 21","wt: 2.32
mpg: 22.8","wt: 3.21
mpg: 21.4","wt: 3.44
mpg: 18.7","wt: 3.46
mpg: 18.1","wt: 3.57
mpg: 14.3","wt: 3.19
mpg: 24.4","wt: 3.15
mpg: 22.8","wt: 3.44
mpg: 19.2","wt: 3.44
mpg: 17.8","wt: 4.07
mpg: 16.4","wt: 3.73
mpg: 17.3","wt: 3.78
mpg: 15.2","wt: 5.25
mpg: 10.4","wt: 5.42
mpg: 10.4","wt: 5.34
mpg: 14.7","wt: 2.2
mpg: 32.4","wt: 1.62
mpg: 30.4","wt: 1.84
mpg: 33.9","wt: 2.46
mpg: 21.5","wt: 3.52
mpg: 15.5","wt: 3.44
mpg: 15.2","wt: 3.84
mpg: 13.3","wt: 3.85
mpg: 19.2","wt: 1.94
mpg: 27.3","wt: 2.14
mpg: 26","wt: 1.51
mpg: 30.4","wt: 3.17
mpg: 15.8","wt: 2.77
mpg: 19.7","wt: 3.57
mpg: 15","wt: 2.78
mpg: 21.4"],"key":null,"type":"scatter","mode":"markers","marker":{"autocolorscale":false,"color":"rgb(0,0,0)","opacity":1,"size":5.66929133858268,"symbol":"circle","line":{"width":1.88976377952756,"color":"rgb(0,0,0)"}},"showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text","name":""},{"x":[1.513,1.56250632911392,1.61201265822785,1.66151898734177,1.7110253164557,1.76053164556962,1.81003797468354,1.85954430379747,1.90905063291139,1.95855696202532,2.00806329113924,2.05756962025316,2.10707594936709,2.15658227848101,2.20608860759494,2.25559493670886,2.30510126582278,2.35460759493671,2.40411392405063,2.45362025316456,2.50312658227848,2.55263291139241,2.60213924050633,2.65164556962025,2.70115189873418,2.7506582278481,2.80016455696203,2.84967088607595,2.89917721518987,2.9486835443038,2.99818987341772,3.04769620253165,3.09720253164557,3.14670886075949,3.19621518987342,3.24572151898734,3.29522784810127,3.34473417721519,3.39424050632911,3.44374683544304,3.49325316455696,3.54275949367089,3.59226582278481,3.64177215189873,3.69127848101266,3.74078481012658,3.79029113924051,3.83979746835443,3.88930379746835,3.93881012658228,3.9883164556962,4.03782278481013,4.08732911392405,4.13683544303798,4.1863417721519,4.23584810126582,4.28535443037975,4.33486075949367,4.3843670886076,4.43387341772152,4.48337974683544,4.53288607594937,4.58239240506329,4.63189873417722,4.68140506329114,4.73091139240506,4.78041772151899,4.82992405063291,4.87943037974684,4.92893670886076,4.97844303797468,5.02794936708861,5.07745569620253,5.12696202531646,5.17646835443038,5.2259746835443,5.27548101265823,5.32498734177215,5.37449367088608,5.424],"y":[29.1989406778126,28.9343555091934,28.6697703405742,28.4051851719549,28.1406000033357,27.8760148347165,27.6114296660973,27.3468444974781,27.0822593288588,26.8176741602396,26.5530889916204,26.2885038230012,26.023918654382,25.7593334857627,25.4947483171435,25.2301631485243,24.9655779799051,24.7009928112859,24.4364076426666,24.1718224740474,23.9072373054282,23.642652136809,23.3780669681898,23.1134817995705,22.8488966309513,22.5843114623321,22.3197262937129,22.0551411250937,21.7905559564744,21.5259707878552,21.261385619236,20.9968004506168,20.7322152819976,20.4676301133783,20.2030449447591,19.9384597761399,19.6738746075207,19.4092894389015,19.1447042702822,18.880119101663,18.6155339330438,18.3509487644246,18.0863635958054,17.8217784271861,17.5571932585669,17.2926080899477,17.0280229213285,16.7634377527093,16.49885258409,16.2342674154708,15.9696822468516,15.7050970782324,15.4405119096132,15.1759267409939,14.9113415723747,14.6467564037555,14.3821712351363,14.1175860665171,13.8530008978978,13.5884157292786,13.3238305606594,13.0592453920402,12.794660223421,12.5300750548017,12.2654898861825,12.0009047175633,11.7363195489441,11.4717343803249,11.2071492117056,10.9425640430864,10.6779788744672,10.413393705848,10.1488085372288,9.88422336860955,9.61963819999033,9.35505303137111,9.09046786275189,8.82588269413267,8.56129752551345,8.29671235689423],"text":["wt: 1.51
mpg: 29.2","wt: 1.56
mpg: 28.93","wt: 1.61
mpg: 28.67","wt: 1.66
mpg: 28.41","wt: 1.71
mpg: 28.14","wt: 1.76
mpg: 27.88","wt: 1.81
mpg: 27.61","wt: 1.86
mpg: 27.35","wt: 1.91
mpg: 27.08","wt: 1.96
mpg: 26.82","wt: 2.01
mpg: 26.55","wt: 2.06
mpg: 26.29","wt: 2.11
mpg: 26.02","wt: 2.16
mpg: 25.76","wt: 2.21
mpg: 25.49","wt: 2.26
mpg: 25.23","wt: 2.31
mpg: 24.97","wt: 2.35
mpg: 24.7","wt: 2.4
mpg: 24.44","wt: 2.45
mpg: 24.17","wt: 2.5
mpg: 23.91","wt: 2.55
mpg: 23.64","wt: 2.6
mpg: 23.38","wt: 2.65
mpg: 23.11","wt: 2.7
mpg: 22.85","wt: 2.75
mpg: 22.58","wt: 2.8
mpg: 22.32","wt: 2.85
mpg: 22.06","wt: 2.9
mpg: 21.79","wt: 2.95
mpg: 21.53","wt: 3
mpg: 21.26","wt: 3.05
mpg: 21","wt: 3.1
mpg: 20.73","wt: 3.15
mpg: 20.47","wt: 3.2
mpg: 20.2","wt: 3.25
mpg: 19.94","wt: 3.3
mpg: 19.67","wt: 3.34
mpg: 19.41","wt: 3.39
mpg: 19.14","wt: 3.44
mpg: 18.88","wt: 3.49
mpg: 18.62","wt: 3.54
mpg: 18.35","wt: 3.59
mpg: 18.09","wt: 3.64
mpg: 17.82","wt: 3.69
mpg: 17.56","wt: 3.74
mpg: 17.29","wt: 3.79
mpg: 17.03","wt: 3.84
mpg: 16.76","wt: 3.89
mpg: 16.5","wt: 3.94
mpg: 16.23","wt: 3.99
mpg: 15.97","wt: 4.04
mpg: 15.71","wt: 4.09
mpg: 15.44","wt: 4.14
mpg: 15.18","wt: 4.19
mpg: 14.91","wt: 4.24
mpg: 14.65","wt: 4.29
mpg: 14.38","wt: 4.33
mpg: 14.12","wt: 4.38
mpg: 13.85","wt: 4.43
mpg: 13.59","wt: 4.48
mpg: 13.32","wt: 4.53
mpg: 13.06","wt: 4.58
mpg: 12.79","wt: 4.63
mpg: 12.53","wt: 4.68
mpg: 12.27","wt: 4.73
mpg: 12","wt: 4.78
mpg: 11.74","wt: 4.83
mpg: 11.47","wt: 4.88
mpg: 11.21","wt: 4.93
mpg: 10.94","wt: 4.98
mpg: 10.68","wt: 5.03
mpg: 10.41","wt: 5.08
mpg: 10.15","wt: 5.13
mpg: 9.88","wt: 5.18
mpg: 9.62","wt: 5.23
mpg: 9.36","wt: 5.28
mpg: 9.09","wt: 5.32
mpg: 8.83","wt: 5.37
mpg: 8.56","wt: 5.42
mpg: 8.3"],"type":"scatter","mode":"lines","name":"fitted values","line":{"width":3.77952755905512,"color":"rgb(51,102,255)","dash":"solid"},"showlegend":false,"xaxis":"x","yaxis":"y","hoverinfo":"text"},{"x":[1.513,1.56250632911392,1.61201265822785,1.66151898734177,1.7110253164557,1.76053164556962,1.81003797468354,1.85954430379747,1.90905063291139,1.95855696202532,2.00806329113924,2.05756962025316,2.10707594936709,2.15658227848101,2.20608860759494,2.25559493670886,2.30510126582278,2.35460759493671,2.40411392405063,2.45362025316456,2.50312658227848,2.55263291139241,2.60213924050633,2.65164556962025,2.70115189873418,2.7506582278481,2.80016455696203,2.84967088607595,2.89917721518987,2.9486835443038,2.99818987341772,3.04769620253165,3.09720253164557,3.14670886075949,3.19621518987342,3.24572151898734,3.29522784810127,3.34473417721519,3.39424050632911,3.44374683544304,3.49325316455696,3.54275949367089,3.59226582278481,3.64177215189873,3.69127848101266,3.74078481012658,3.79029113924051,3.83979746835443,3.88930379746835,3.93881012658228,3.9883164556962,4.03782278481013,4.08732911392405,4.13683544303798,4.1863417721519,4.23584810126582,4.28535443037975,4.33486075949367,4.3843670886076,4.43387341772152,4.48337974683544,4.53288607594937,4.58239240506329,4.63189873417722,4.68140506329114,4.73091139240506,4.78041772151899,4.82992405063291,4.87943037974684,4.92893670886076,4.97844303797468,5.02794936708861,5.07745569620253,5.12696202531646,5.17646835443038,5.2259746835443,5.27548101265823,5.32498734177215,5.37449367088608,5.424,5.424,5.424,5.37449367088608,5.32498734177215,5.27548101265823,5.2259746835443,5.17646835443038,5.12696202531646,5.07745569620253,5.02794936708861,4.97844303797468,4.92893670886076,4.87943037974684,4.82992405063291,4.78041772151899,4.73091139240506,4.68140506329114,4.63189873417722,4.58239240506329,4.53288607594937,4.48337974683544,4.43387341772152,4.3843670886076,4.33486075949367,4.28535443037975,4.23584810126582,4.1863417721519,4.13683544303798,4.08732911392405,4.03782278481013,3.9883164556962,3.93881012658228,3.88930379746835,3.83979746835443,3.79029113924051,3.74078481012658,3.69127848101266,3.64177215189873,3.59226582278481,3.54275949367089,3.49325316455696,3.44374683544304,3.39424050632911,3.34473417721519,3.29522784810127,3.24572151898734,3.19621518987342,3.14670886075949,3.09720253164557,3.04769620253165,2.99818987341772,2.9486835443038,2.89917721518987,2.84967088607595,2.80016455696203,2.7506582278481,2.70115189873418,2.65164556962025,2.60213924050633,2.55263291139241,2.50312658227848,2.45362025316456,2.40411392405063,2.35460759493671,2.30510126582278,2.25559493670886,2.20608860759494,2.15658227848101,2.10707594936709,2.05756962025316,2.00806329113924,1.95855696202532,1.90905063291139,1.85954430379747,1.81003797468354,1.76053164556962,1.7110253164557,1.66151898734177,1.61201265822785,1.56250632911392,1.513,1.513],"y":[31.4341217313206,31.1204993874088,30.8072470380303,30.4943905399572,30.1819579750846,29.8699798618842,29.5584893857579,29.2475226490581,28.9371189411946,28.6273210287383,28.3181754647066,28.0097329152165,27.7020485003464,27.3951821442752,27.0891989274822,26.7841694309033,26.4801700583702,26.1772833193625,25.8755980490886,25.5752095372801,25.276219531088,24.9787360715631,24.6828731181045,24.3887499120139,24.096490030255,23.8062200853351,23.5180680386105,23.2321611137817,22.9486233256676,22.6675726760022,22.3891181105935,22.1133563760938,21.8403689531932,21.5702192684414,21.3029503917649,21.0385834062175,20.7771165901197,20.5185254843377,20.2627638386563,20.0097653533643,19.7594460674074,19.5117072017912,19.266438250352,19.023520118358,18.7828281372767,18.5442348238948,18.307612296075,18.0728342993708,17.8397778342311,17.6083244005099,17.3783608941716,17.1497802013801,16.9224815391307,16.6963705909696,16.4713594827389,16.2473666380091,16.0243165469005,15.8021394760335,15.5807711417929,15.3601523641871,15.1402287143902,14.9209501655981,14.7022707540356,14.4841482547477,14.266543875108,14.0494219676868,13.8327497631713,13.6164971233358,13.400636313583,13.185141794249,12.96999002966,12.7551593138153,12.5406296115153,12.3263824137488,12.1124006061819,11.8986683496337,11.685170971489,11.4718948670624,11.2588274100015,11.0459568708893,5.5474678428992,5.5474678428992,5.86376764102542,6.17987052120298,6.49576475401476,6.81143771310854,7.1268757937988,7.44206432347029,7.75698746294225,8.07162809788066,8.38596771927446,8.69998629192387,9.01366210982825,9.32697163731397,9.6398893347169,9.95238746743981,10.2644358972571,10.5760018548558,10.8870496928064,11.1975406184823,11.5074324069286,11.8166790943701,12.1252306540028,12.4330326570007,12.740025923372,13.0461461695019,13.3513236620105,13.6554828910183,13.9585422800956,14.2604139550846,14.5610035995316,14.8602104304317,15.157927333949,15.4540412060478,15.748433546582,16.0409813560006,16.3315583798572,16.6200367360143,16.9062889412588,17.190190327058,17.4716217986802,17.7504728499618,18.0266447019082,18.3000533934652,18.5706326249216,18.8383361460623,19.1031394977534,19.3650409583153,19.624061610802,19.8802445251398,20.1336531278785,20.3843688997083,20.6324885872813,20.8781211364057,21.1213845488153,21.3624028393291,21.6013032316477,21.8382136871272,22.073260818275,22.3065682020549,22.5382550797684,22.7684354108148,22.9972172362447,23.2247023032092,23.4509859014399,23.6761568661453,23.9002977068049,24.1234848272503,24.3457888084175,24.5672747307859,24.7880025185342,25.0080272917409,25.2273997165231,25.4461663458981,25.6643699464367,25.8820498075489,26.0992420315869,26.3159798039527,26.532293643118,26.748211630978,26.9637596243046,31.4341217313206],"text":["wt: 1.51
mpg: 29.2","wt: 1.56
mpg: 28.93","wt: 1.61
mpg: 28.67","wt: 1.66
mpg: 28.41","wt: 1.71
mpg: 28.14","wt: 1.76
mpg: 27.88","wt: 1.81
mpg: 27.61","wt: 1.86
mpg: 27.35","wt: 1.91
mpg: 27.08","wt: 1.96
mpg: 26.82","wt: 2.01
mpg: 26.55","wt: 2.06
mpg: 26.29","wt: 2.11
mpg: 26.02","wt: 2.16
mpg: 25.76","wt: 2.21
mpg: 25.49","wt: 2.26
mpg: 25.23","wt: 2.31
mpg: 24.97","wt: 2.35
mpg: 24.7","wt: 2.4
mpg: 24.44","wt: 2.45
mpg: 24.17","wt: 2.5
mpg: 23.91","wt: 2.55
mpg: 23.64","wt: 2.6
mpg: 23.38","wt: 2.65
mpg: 23.11","wt: 2.7
mpg: 22.85","wt: 2.75
mpg: 22.58","wt: 2.8
mpg: 22.32","wt: 2.85
mpg: 22.06","wt: 2.9
mpg: 21.79","wt: 2.95
mpg: 21.53","wt: 3
mpg: 21.26","wt: 3.05
mpg: 21","wt: 3.1
mpg: 20.73","wt: 3.15
mpg: 20.47","wt: 3.2
mpg: 20.2","wt: 3.25
mpg: 19.94","wt: 3.3
mpg: 19.67","wt: 3.34
mpg: 19.41","wt: 3.39
mpg: 19.14","wt: 3.44
mpg: 18.88","wt: 3.49
mpg: 18.62","wt: 3.54
mpg: 18.35","wt: 3.59
mpg: 18.09","wt: 3.64
mpg: 17.82","wt: 3.69
mpg: 17.56","wt: 3.74
mpg: 17.29","wt: 3.79
mpg: 17.03","wt: 3.84
mpg: 16.76","wt: 3.89
mpg: 16.5","wt: 3.94
mpg: 16.23","wt: 3.99
mpg: 15.97","wt: 4.04
mpg: 15.71","wt: 4.09
mpg: 15.44","wt: 4.14
mpg: 15.18","wt: 4.19
mpg: 14.91","wt: 4.24
mpg: 14.65","wt: 4.29
mpg: 14.38","wt: 4.33
mpg: 14.12","wt: 4.38
mpg: 13.85","wt: 4.43
mpg: 13.59","wt: 4.48
mpg: 13.32","wt: 4.53
mpg: 13.06","wt: 4.58
mpg: 12.79","wt: 4.63
mpg: 12.53","wt: 4.68
mpg: 12.27","wt: 4.73
mpg: 12","wt: 4.78
mpg: 11.74","wt: 4.83
mpg: 11.47","wt: 4.88
mpg: 11.21","wt: 4.93
mpg: 10.94","wt: 4.98
mpg: 10.68","wt: 5.03
mpg: 10.41","wt: 5.08
mpg: 10.15","wt: 5.13
mpg: 9.88","wt: 5.18
mpg: 9.62","wt: 5.23
mpg: 9.36","wt: 5.28
mpg: 9.09","wt: 5.32
mpg: 8.83","wt: 5.37
mpg: 8.56","wt: 5.42
mpg: 8.3","wt: 5.42
mpg: 8.3","wt: 5.42
mpg: 8.3","wt: 5.37
mpg: 8.56","wt: 5.32
mpg: 8.83","wt: 5.28
mpg: 9.09","wt: 5.23
mpg: 9.36","wt: 5.18
mpg: 9.62","wt: 5.13
mpg: 9.88","wt: 5.08
mpg: 10.15","wt: 5.03
mpg: 10.41","wt: 4.98
mpg: 10.68","wt: 4.93
mpg: 10.94","wt: 4.88
mpg: 11.21","wt: 4.83
mpg: 11.47","wt: 4.78
mpg: 11.74","wt: 4.73
mpg: 12","wt: 4.68
mpg: 12.27","wt: 4.63
mpg: 12.53","wt: 4.58
mpg: 12.79","wt: 4.53
mpg: 13.06","wt: 4.48
mpg: 13.32","wt: 4.43
mpg: 13.59","wt: 4.38
mpg: 13.85","wt: 4.33
mpg: 14.12","wt: 4.29
mpg: 14.38","wt: 4.24
mpg: 14.65","wt: 4.19
mpg: 14.91","wt: 4.14
mpg: 15.18","wt: 4.09
mpg: 15.44","wt: 4.04
mpg: 15.71","wt: 3.99
mpg: 15.97","wt: 3.94
mpg: 16.23","wt: 3.89
mpg: 16.5","wt: 3.84
mpg: 16.76","wt: 3.79
mpg: 17.03","wt: 3.74
mpg: 17.29","wt: 3.69
mpg: 17.56","wt: 3.64
mpg: 17.82","wt: 3.59
mpg: 18.09","wt: 3.54
mpg: 18.35","wt: 3.49
mpg: 18.62","wt: 3.44
mpg: 18.88","wt: 3.39
mpg: 19.14","wt: 3.34
mpg: 19.41","wt: 3.3
mpg: 19.67","wt: 3.25
mpg: 19.94","wt: 3.2
mpg: 20.2","wt: 3.15
mpg: 20.47","wt: 3.1
mpg: 20.73","wt: 3.05
mpg: 21","wt: 3
mpg: 21.26","wt: 2.95
mpg: 21.53","wt: 2.9
mpg: 21.79","wt: 2.85
mpg: 22.06","wt: 2.8
mpg: 22.32","wt: 2.75
mpg: 22.58","wt: 2.7
mpg: 22.85","wt: 2.65
mpg: 23.11","wt: 2.6
mpg: 23.38","wt: 2.55
mpg: 23.64","wt: 2.5
mpg: 23.91","wt: 2.45
mpg: 24.17","wt: 2.4
mpg: 24.44","wt: 2.35
mpg: 24.7","wt: 2.31
mpg: 24.97","wt: 2.26
mpg: 25.23","wt: 2.21
mpg: 25.49","wt: 2.16
mpg: 25.76","wt: 2.11
mpg: 26.02","wt: 2.06
mpg: 26.29","wt: 2.01
mpg: 26.55","wt: 1.96
mpg: 26.82","wt: 1.91
mpg: 27.08","wt: 1.86
mpg: 27.35","wt: 1.81
mpg: 27.61","wt: 1.76
mpg: 27.88","wt: 1.71
mpg: 28.14","wt: 1.66
mpg: 28.41","wt: 1.61
mpg: 28.67","wt: 1.56
mpg: 28.93","wt: 1.51
mpg: 29.2","wt: 1.51
mpg: 29.2"],"type":"scatter","mode":"lines","line":{"width":3.77952755905512,"color":"transparent","dash":"solid"},"fill":"tozerox","fillcolor":"rgba(153,153,153,0.4)","hoverinfo":"x+y","showlegend":false,"xaxis":"x","yaxis":"y","name":""}],"layout":{"margin":{"b":40.6475716064757,"l":40.6475716064757,"t":23.9701120797011,"r":7.97011207970112},"plot_bgcolor":"rgb(255,255,255)","paper_bgcolor":"rgb(255,255,255)","font":{"color":"rgb(0,0,0)","family":"","size":15.9402241594022},"xaxis":{"type":"linear","autorange":false,"tickmode":"array","range":[1.31745,5.61955],"ticktext":["2","3","4","5"],"tickvals":[2,3,4,5],"ticks":"outside","tickcolor":"rgb(0,0,0)","ticklen":3.98505603985056,"tickwidth":0.66417600664176,"showticklabels":true,"tickfont":{"color":"rgb(0,0,0)","family":"","size":12.7521793275218},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0,1],"gridcolor":"rgb(229,229,229)","gridwidth":0.265670402656704,"zeroline":false,"anchor":"y","hoverformat":".2f"},"annotations":[{"text":"Weight (1000lbs)","x":0.5,"y":-0.10008763433421,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgb(0,0,0)","family":"","size":15.9402241594022},"xref":"paper","yref":"paper","textangle":-0,"xanchor":"center","yanchor":"middle"},{"text":"Miles per Gallon","x":-0.10008763433421,"y":0.5,"showarrow":false,"ax":0,"ay":0,"font":{"color":"rgb(0,0,0)","family":"","size":15.9402241594022},"xref":"paper","yref":"paper","textangle":-90,"xanchor":"center","yanchor":"middle"}],"yaxis":{"type":"linear","autorange":false,"tickmode":"array","range":[4.12984123504415,35.317626607855],"ticktext":["10","20","30"],"tickvals":[10,20,30],"ticks":"outside","tickcolor":"rgb(0,0,0)","ticklen":3.98505603985056,"tickwidth":0.66417600664176,"showticklabels":true,"tickfont":{"color":"rgb(0,0,0)","family":"","size":12.7521793275218},"tickangle":-0,"showline":false,"linecolor":null,"linewidth":0,"showgrid":true,"domain":[0,1],"gridcolor":"rgb(229,229,229)","gridwidth":0.265670402656704,"zeroline":false,"anchor":"x","hoverformat":".2f"},"shapes":[{"type":"rect","fillcolor":"transparent","line":{"color":"rgb(127,127,127)","width":0.66417600664176,"linetype":"solid"},"yref":"paper","xref":"paper","x0":0,"x1":1,"y0":0,"y1":1}],"showlegend":false,"legend":{"bgcolor":"rgb(255,255,255)","bordercolor":"transparent","borderwidth":1.88976377952756,"font":{"color":"rgb(0,0,0)","family":"","size":12.7521793275218}},"hovermode":"closest"},"source":"A","config":{"modeBarButtonsToRemove":["sendDataToCloud"]},"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>

It appears that with a gain in every 1000 pounds that there will be a decrease in fuel efficiency by 5.3444716 miles per gallon

The end


If we use literate programming, we could also:

  1. Tangle: Extract the source code out of the document.
  2. Weave: Execute the code to get the compiled results.

Reproducible Research

Reproducible research is one possible product of dynamic documents, however, it is not guaranteed! Good practices for reproducible research include:

  1. Encapsulate the full project into one directory that is supported with version control.
  2. Release your code and data.
  3. Document everything and use code as documentation!
  4. Make figures, tables, and statistics the results of scripts and inline code.
  5. Write code that uses relative paths.
  6. Always Set your seed.
  7. Always include session information in the code file. For example, you can use devtools::session_info().

To read more about reproducibility and data management check out Vince Buffalo's Book[@Buffalo2015].


Markdown

To fully understand RMarkdown, we first need to cover Markdown, which is a system for writing simple, readable text that is easily converted to HTML. Markdown essentially is two things:

  1. A plain text formatting syntax
  2. A software tool written in Perl.
    • Converts the plain text formatting into HTML.

Main goal of Markdown:
Make the syntax of the raw (pre-HTML) document as readable possible.

Would you rather read this code in HTML?

<body>
  <section>
    <h1>Rock Climbing Packing List</h1>
    <ul>
      <li>Climbing Shoes</li>
      <li>Harness</li>
      <li>Backpack</li>
      <li>Rope</li>
      <li>Belayer</li>
    </ul>
  </section>
</body>

Or this code in Markdown?

# Rock Climbing Packing List

* Climbing Shoes
* Harness
* Backpack  
* Rope
* Belayer

If you are human, the Markdown code is definitely easier to read!

We will talk more about the syntax of Markdown after we introduce RMarkdown but let us take a moment to soak in how much easier our lives are/will be because Markdown exists! Thank you John Gruber and Aaron Swartz (RIP) for creating Markdown in 2004!


RMarkdown

RMarkdown is a variant of Markdown that makes it easy to create dynamic documents, presentations and reports within RStudio. It has embedded R code chunks to be used with knitr to make it easy to create reproducible (web-based) reports in the sense that they can be automatically regenerated when the underlying code it modified.

  • RMarkdown lets you combine Markdown with images, links, tables, LaTeX, and actual code.
  • RStudio makes creating documents from RMarkdown easy
  • RStudio (like R) is free and runs on any operating system.

RMarkdown renders many different types of files including:

While there are a lot of different types of rendered documents in RMarkdown, today we will focus primarily on HTML output files, as I have found these files to be the most useful and flexible for my research.

Why R Markdown?

A convenient tool for reproducible and dynamic reports!

  • While it was created for R, it now accepts many programming languages. For simplicity, we will only work with R today.
  • Execute code in a few ways:
    1. Inline Code: Brief code that takes place during the written part of the document.
    2. Code Chunks: Parts of the document that includes several lines of program or analysis code. It may render a plot or table, calculate summary statistics, load packages, etc.
  • It is easy to:
    • Embed images.
    • Learn Markdown syntax.
    • Include LaTeX equations.
    • Include interactive tables.
    • Use version control with Git.
      • Even easier to share and collaborate on analyses, projects and publications!
    • Add external links - Rmarkdown even understands some html code!
    • Make beautifully formatted documents.
  • Do not 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

Simple Workflow

Briefly, to make a report:

  1. Open a .Rmd file.
    • Create a YAML header (more on this in a minute!)
  2. Write the content with RMarkdown syntax.
  3. Embed the R code in code chunks or inline code.
  4. Render the document output.

Workflow for creating a report

Overview of the steps RMarkdown takes to get to the rendered document:

  1. Create .Rmd report that includes R code chunks and and markdown narratives (as indicated in steps above.).
  2. Give the .Rmd file to knitr to execute the R code chunks and create a new .md file.
    • Knitr is a package within R that allows the integration of R code into rendered RMarkdown documents such as HTML, latex, pdf, word, among other document types.
  3. Give the .md file to pandoc, which will create the final rendered document (e.g. html, Microsoft word, pdf, etc.).
    • Pandoc is a universal document converter and enables the conversion of one document type (in this case: .Rmd) to another (in this case: HTML)

How an Rmd document is rendered

While this may seem complicated, we can hit the "Knit" button at the top of the page like this:

or we can run the following code:

rmarkdown::render("RMarkdown_Lesson.Rmd", "html_document")

Creating a .Rmd File

It's go time! Let's start working with RMarkdown!

  1. In the menu bar, click File -> New File -> RMarkdown
    • Or simply click on the green plus sign in the top left corner of RStudio.

  1. The window below will pop up.
  • Inside of this window, choose the type of output by selecting the radio buttons. Note: this output can be easily changed later!

  1. Click OK

YAML Headers

YAML stands for "YAML Ain't Markup Language" and is basically a nested list structure that includes the metadata of the document. It is enclosed between two lines of three dashes --- and as we saw above is automatically written by RStudio. A simple example:

---
title:  "Analysis Report"  
Author:  "Marian L. Schmidt"  
date: "May 11th, 2016"  
output:  html_document
---

The above example will create an HTML document. However, the following options are also available.

  • html_document
  • pdf_document
  • word_document
  • beamer_presentation (pdf slideshow)
  • ioslides_presentation (HTML slideshow)
  • and more...

Today, we will be focused on HTML files. However, please be welcome to play around with creating word and pdf documents. Presentation slides take on a slightly different syntax (e.g. to specify when one slide ends and the next one starts) and so there is a bit of markdown syntax specific to presentations that are beyond the focus of this workshop.

We will build on the details of YAML headers throughout the workshop.

Markdown Basics

Check out the RMarkdown Reference Guide

To list a few from the RMarkdown Cheatsheet:

Markdown Basics from RStudio's RMarkdown Cheatsheet

Helpful Hints:

  • End a line with two spaces to start a new paragraph.
  • Words formatted like code should be surrounded by back ticks on both sides: `
  • To make something superscript surround it with ^ on each side. Super^script^ was created by typing Super^script^.
  • Equations can be inline code using $ and centered as a blocked equation within the document with $$. For example $E = mc^2$ is inline while the following is a blocked equation is: $$E = mc^2$$
    • Note: To make it superscript with $ and $$ a ^ is needed before each alphanumeric that is superscript.
    • Other fun math stuff:
      • Square root: $\sqrt{b}$ will create $\sqrt{b}$
      • Fractions: $\frac{1}{2}$ = $\frac{1}{2}$
          • Fractional Equations: $f(x)=\frac{P(x)}{Q(x)}$ = $f(x)=\frac{P(x)}{Q(x)}$
      • Binomial Coefficients: $\binom{k}{n}$ = $\binom{k}{n}$
      • Integrals: $$\int_{a}^{b} x^2 dx$$ = $$\int_{a}^{b} x^2 dx$$
    • ShareLaTeX is an awesome source for LaTeX code.

Some more mathy stuff:

Description Code Examples
Greek letters $\alpha$ $\beta$ $\gamma$ $\rho$ $\sigma$ $\delta$ $\epsilon$ $mu$ $\alpha$ $\beta$ $\gamma$ $\rho$ $\sigma$ $\delta$ $\epsilon$ $\mu$
Binary operators $\times$ $\otimes$ $\oplus$ $\cup$ $\cap$ $\times$ $\otimes$ $\oplus$ $\cup$ $\cap$ $\times$
Relation operators $< >$ $\subset$ $\supset$ $\subseteq$ $\supseteq$ $&lt; &gt;$ $\subset$ $\supset$ $\subseteq$ $\supseteq$
Others $\int$ $\oint$ $\sum$ $\prod$ $\int$ $\oint$ $\sum$ $\prod$

Challenge: Try to mimic the output of the following:

  1. Today, I am bold and am learning RMarkdown.

  2. honey is very sweet.

  3. YAS!!!^!!!^

  4. R^2^ values are informative!

  5. $R^{2}$ describe the variance explained in the model.

  6. I do not know RMarkdown Today I learned RMarkdown

  7. RStudio link

  8. Output from the following:

# RMarkdown   
## R   
### Knitr   
#### Pandoc  
##### HTML  
  1. $\sqrt{b^2 - 4ac}$

  2. $$\sqrt{b^2 - 4ac}$$

  3. $X_{i,j}$

  1. Today is the day I will make a dynamic document!
  1. The following list:

Chocolate Chip cookie Recipe

  1. butter
  2. sugar
    • A mix of brown & white sugar makes it more delicious
      • mix with butter before you add the eggs
  3. eggs
  4. vanilla
  5. Mix dry ingredients:
    • flour, salt, baking soda
  6. chocolate chips

Fun Fact! The table of contents of this website was created from headers with 1-3 pound symbols! (More on this later)

Embed Code

There are 2 ways to embed code within an RMarkdown document.

  1. Inline Code: Brief code that takes place during the written part of the document.

  2. Code Chunks: Parts of the document that includes several lines of program or analysis code. It may render a plot or table, calculate summary statistics, load packages, etc.

Inline R Code

Inline code is created by using a back tick (`) and the letter r followed by another back tick.

  • For example: 2^11^ is 2048.

Imagine that you're reporting a p-value and you do not want to go back and add it every time the statistical test is re-run. Rather, the p-value is 0.0045.

This is really helpful when writing up the results section of a paper. For example, you may have ran a bunch of statistics for your scientific questions and this would be a way to have R save that value in a variable name.

For example: Is the gas mileage of automatic versus manual transmissions significantly different within the mtcars data set?

mpg_auto <- mtcars[mtcars$am == 0,]$mpg # automatic transmission mileage
mpg_manual <- mtcars[mtcars$am == 1,]$mpg # manual transmission mileage
transmission_ttest <- t.test(mpg_auto, mpg_manual)

To extract the p-value we can type transmission_ttest$p.value within inline code.

The p-value is 0.0013736.

R Code Chunks

R code chunks can be used to render R output into documents or to display code for illustration.

The Anatomy of a code chunk:

To insert an R code chunk, you can type it manually by typing ```{r} followed by ``` on the next line. You can also press the Insert a new code chunk button or use the shortcut key. This will produce the following code chunk:

Inserting a code chunk

`�``{r}
n <- 10
seq(n)
```

Name the code chunk something meaningful as to what it is doing. Below I have named the code chunk 10-random-numbers:

`�``{r 10-random-numbers}
n <- 10
seq(n)
```

The code chunk input and output is then displayed as follows:

n = 10
seq(n)
##  [1]  1  2  3  4  5  6  7  8  9 10

Knitr

Knitr is an R-Package that works with

  1. Identifies code including chunks and inline
  2. Evaluates all the code and returns the results
  3. Renders a formatted results and combines with original file.

Knitr runs code as if it were being run in the R console.

Mainly Knitr works with code chunks.

A code chunk looks like:

```r
x <- rnorm(100)  
y <- 2*x + rnorm(100)
```

Best practices regarding code chunks:

  1. Always name/label your code chunks!
  2. Instead of specifying the chunk options in every chunk, set the global chunk options at the beginning of the document. More on this in a minute!

Chunk Labels

Chunk labels must be unique IDs in a document and are good for:

  • Generating external files such as images and cached documents.
  • Chunk labels often are output when errors arise (more often for line of code).
  • Navigating throughout long .Rmd documents.

A method of navigating through .Rmd files

When naming the code chunk: Use - or _ in between words for code chunks labels instead of spaces. This will help you and other users of your document to navigate through.

Chunk labels must be unique throughout the document - otherwise there will be an error!

Chunk Options

Pressing tab when inside the braces will bring up code chunk options.

Some Knitr Chunk Options

  • results = "asis" stands for "as is" and will output a non-formatted version.
  • collapse is another chunk option which can be helpful. If a code chunk has many short R expressions with some output, you can collapse the output into a chunk.

There are too many chunk options to cover here. After the workshop take a look around at the options.

Great website for exploring Knitr Chunk Options.

Challenge
Run the code chunk below and play with the following knitr code chunk options:

  • eval = TRUE/FALSE
  • echo = TRUE/FALSE
  • collapse = TRUE/FALSE
  • results = "asis","markup, and "hide

In markdown, record your results.
Note: Be sure to name your chunks!

1+1
2*5
seq(1, 21, by = 3)
head(mtcars)

Some examples from the chunk above

Results from results="markup", collapse = TRUE}:

1+1
## [1] 2
2*5
## [1] 10
seq(1, 21, by = 3)
## [1]  1  4  7 10 13 16 19
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Results from results="asis", collapse = TRUE}:

1+1

[1] 2

2*5

[1] 10

seq(1, 21, by = 3)

[1] 1 4 7 10 13 16 19

head(mtcars)
               mpg cyl disp  hp drat    wt  qsec vs am gear carb

Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

Global Options

You may wish to have the same chunk settings throughout your document and so it might be nice to type options once instead of always re-typing it for each chunk. To do so, you can set global chunk options at the top of the document.

knitr::opts_chunk$set(echo = FALSE, 
                      eval = TRUE, 
                      message = FALSE,
                      warning = FALSE, 
                      fig.path = "Figures/",
                      fig.width = 12, 
                      fig.height = 8)

For example, if you're working with a collaborator who does not want to see the code - you could set eval = TRUE and echo = FALSE so the code is evaluated but not shown. In addition, you may want to use message = FALSE and warning = FALSE so your collaborator does not see any messages or warnings from R.

If you would like to save and store figures within a sub directory within the project, fig.path = "Figures/". Here, the "Figures/" denotes a folder named Figures within the current directory where the figures produced within the document will be stored. Note: by default figures are not saved.

Global chunk options will be set for the rest of the document. If you would like to have a particular chunk be different from the global options, specify at the beginning of that particular chunk.

Figures

Knitr makes producing figures really easy. If analysis code within a chunk is supposed to produce a figure, it will just print out into the document.

Some knitr chunk options that relate to figures:

  • fig.width and fig.height
    • Default: fig.width = 7, fig.height = 7
  • fig.align: How to align the figure
    • Options include: "left", "right", and "center"
  • fig.path: A file path to the directory to where knitr should store the graphic output created by the chunk.
    • Default: 'figure/'
  • There's even a fig.retina(only for HTML output) for higher figure resolution with retina displays.

Making a single figure:

With fig.align = "center"

ggplot(mtcars, aes(x = mpg)) + xlab("Miles per Gallon") +
    geom_histogram(bins = 30, fill = "cornflowerblue", color = "black") +
    geom_vline(xintercept=mean(mtcars$mpg), col="red") 

With fig.align = "right"

ggplot(mtcars, aes(x = mpg)) + xlab("Miles per Gallon") +
    geom_histogram(bins = 30, fill = "cornflowerblue", color = "black") +
    geom_vline(xintercept=mean(mtcars$mpg), col="red") 

With fig.align = "left"

ggplot(mtcars, aes(x = mpg)) + xlab("Miles per Gallon") +
    geom_histogram(bins = 30, fill = "cornflowerblue", color = "black") +
    geom_vline(xintercept=mean(mtcars$mpg), col="red") 

With fig.width = 2, fig.height = 2

ggplot(mtcars, aes(x = mpg)) + xlab("Miles per Gallon") +
    geom_histogram(bins = 30, fill = "cornflowerblue", color = "black") +
    geom_vline(xintercept=mean(mtcars$mpg), col="red") 

With fig.width = 10, fig.height = 10

ggplot(mtcars, aes(x = mpg)) + xlab("Miles per Gallon") +
    geom_histogram(bins = 30, fill = "cornflowerblue", color = "black") +
    geom_vline(xintercept=mean(mtcars$mpg), col="red") 
myplots <- list()  # new empty list
for(i in 1:ncol(mtcars)){
  col <- names(mtcars)[i]
  ggp <- ggplot(mtcars, aes_string(x = col)) +
    geom_histogram(bins = 30, fill = "cornflowerblue", color = "black") +
    geom_vline(xintercept = mean(mtcars[[col]]), col = "red") 
  myplots[[i]] <- ggp  # add each plot into plot list
}
multiplot(plotlist = myplots, cols = 4) # must load in multiplot function from the Rcookbook see http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/

Tables

Hand writing tables in Markdown can get tedious. We will not go over this here, however, if you'd like to learn more about Markdown tables check out the documentation on tables at the RMarkdown v2 website.

There's a few types of tables that are useful. Here, we will use our previous example of a looking at the mtcars data

In his Knitr in a Knutshell, Dr. Karl Broman introduces: kable, pander, and xtable and I have especially enjoyed using the first two:

  • kable: Within the knitr package - not many options but looks nice with ease.
  • pander: Within the pander package - has many more options and customization. Useful for bold-ing certain values (e.g. values below a threshold).

kable and pander tables are nice as they are useful for making non-interactive tables:

kable(head(mtcars, n = 4)) # kable table with 4 rows
               mpg   cyl   disp    hp   drat      wt    qsec   vs   am   gear   carb

Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1

# Pander table
# install.packages("pander") # install pander first
library(pander)
pander(head(mtcars, n = 4))

   &nbsp;         mpg   cyl   disp   hp   drat   wt    qsec   vs   am   gear   carb 

Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4

Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4

Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1

Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1

HTML Widgets

With the release of the new RMarkdown v2 it is now easier than ever to include HTML Widgets. Follow the link to explore widgets you're interested in!

Recently, I discovered the DT package which makes the tables interactive in the HTML output. In addition, Plotly for R produces really beautiful interactive graphics based on Plotly.

Cool, huh?

# DT table = interactive
# install.packages("DT") # install DT first
library(DT)
datatable(head(mtcars, n = nrow(mtcars)), options = list(pageLength = 5)) 
<script type="application/json" data-for="htmlwidget-3241">{"x":{"data":[["Mazda RX4","Mazda RX4 Wag","Datsun 710","Hornet 4 Drive","Hornet Sportabout","Valiant","Duster 360","Merc 240D","Merc 230","Merc 280","Merc 280C","Merc 450SE","Merc 450SL","Merc 450SLC","Cadillac Fleetwood","Lincoln Continental","Chrysler Imperial","Fiat 128","Honda Civic","Toyota Corolla","Toyota Corona","Dodge Challenger","AMC Javelin","Camaro Z28","Pontiac Firebird","Fiat X1-9","Porsche 914-2","Lotus Europa","Ford Pantera L","Ferrari Dino","Maserati Bora","Volvo 142E"],[21,21,22.8,21.4,18.7,18.1,14.3,24.4,22.8,19.2,17.8,16.4,17.3,15.2,10.4,10.4,14.7,32.4,30.4,33.9,21.5,15.5,15.2,13.3,19.2,27.3,26,30.4,15.8,19.7,15,21.4],[6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4],[160,160,108,258,360,225,360,146.7,140.8,167.6,167.6,275.8,275.8,275.8,472,460,440,78.7,75.7,71.1,120.1,318,304,350,400,79,120.3,95.1,351,145,301,121],[110,110,93,110,175,105,245,62,95,123,123,180,180,180,205,215,230,66,52,65,97,150,150,245,175,66,91,113,264,175,335,109],[3.9,3.9,3.85,3.08,3.15,2.76,3.21,3.69,3.92,3.92,3.92,3.07,3.07,3.07,2.93,3,3.23,4.08,4.93,4.22,3.7,2.76,3.15,3.73,3.08,4.08,4.43,3.77,4.22,3.62,3.54,4.11],[2.62,2.875,2.32,3.215,3.44,3.46,3.57,3.19,3.15,3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.2,1.615,1.835,2.465,3.52,3.435,3.84,3.845,1.935,2.14,1.513,3.17,2.77,3.57,2.78],[16.46,17.02,18.61,19.44,17.02,20.22,15.84,20,22.9,18.3,18.9,17.4,17.6,18,17.98,17.82,17.42,19.47,18.52,19.9,20.01,16.87,17.3,15.41,17.05,18.9,16.7,16.9,14.5,15.5,14.6,18.6],[0,0,1,1,0,1,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,1],[1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1],[4,4,4,3,3,3,3,4,4,4,4,3,3,3,3,3,3,4,4,4,3,3,3,3,3,4,5,5,5,5,5,4],[4,4,1,1,2,1,4,2,2,4,4,3,3,3,4,4,4,1,2,1,1,2,2,4,2,1,2,2,4,6,8,2]],"container":"\n \n \n
\u003c/th>\n mpg\u003c/th>\n cyl\u003c/th>\n disp\u003c/th>\n hp\u003c/th>\n drat\u003c/th>\n wt\u003c/th>\n qsec\u003c/th>\n vs\u003c/th>\n am\u003c/th>\n gear\u003c/th>\n carb\u003c/th>\n \u003c/tr>\n \u003c/thead>\n\u003c/table>","options":{"pageLength":5,"columnDefs":[{"className":"dt-right","targets":[1,2,3,4,5,6,7,8,9,10,11]},{"orderable":false,"targets":0}],"order":[],"autoWidth":false,"orderClasses":false,"lengthMenu":[5,10,25,50,100]},"callback":null,"filter":"none"},"evals":[],"jsHooks":[]}</script>
# plotly
# install.packages("plotly")
library(plotly)
mtcars$car <- row.names(mtcars)
plot_ly(mtcars, x = wt, y = mpg, 
        text = paste("Car: ", car),
        mode = "markers", color = wt, size = wt)
<script type="application/json" data-for="htmlwidget-1748">{"x":{"data":[{"type":"scatter","inherit":false,"x":[2.62,2.875,2.32,3.215,3.44,3.46,3.57,3.19,3.15,3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.2,1.615,1.835,2.465,3.52,3.435,3.84,3.845,1.935,2.14,1.513,3.17,2.77,3.57,2.78],"y":[21,21,22.8,21.4,18.7,18.1,14.3,24.4,22.8,19.2,17.8,16.4,17.3,15.2,10.4,10.4,14.7,32.4,30.4,33.9,21.5,15.5,15.2,13.3,19.2,27.3,26,30.4,15.8,19.7,15,21.4],"text":["Car: Mazda RX4
wt (size): 2.62","Car: Mazda RX4 Wag
wt (size): 2.875","Car: Datsun 710
wt (size): 2.32","Car: Hornet 4 Drive
wt (size): 3.215","Car: Hornet Sportabout
wt (size): 3.44","Car: Valiant
wt (size): 3.46","Car: Duster 360
wt (size): 3.57","Car: Merc 240D
wt (size): 3.19","Car: Merc 230
wt (size): 3.15","Car: Merc 280
wt (size): 3.44","Car: Merc 280C
wt (size): 3.44","Car: Merc 450SE
wt (size): 4.07","Car: Merc 450SL
wt (size): 3.73","Car: Merc 450SLC
wt (size): 3.78","Car: Cadillac Fleetwood
wt (size): 5.25","Car: Lincoln Continental
wt (size): 5.424","Car: Chrysler Imperial
wt (size): 5.345","Car: Fiat 128
wt (size): 2.2","Car: Honda Civic
wt (size): 1.615","Car: Toyota Corolla
wt (size): 1.835","Car: Toyota Corona
wt (size): 2.465","Car: Dodge Challenger
wt (size): 3.52","Car: AMC Javelin
wt (size): 3.435","Car: Camaro Z28
wt (size): 3.84","Car: Pontiac Firebird
wt (size): 3.845","Car: Fiat X1-9
wt (size): 1.935","Car: Porsche 914-2
wt (size): 2.14","Car: Lotus Europa
wt (size): 1.513","Car: Ford Pantera L
wt (size): 3.17","Car: Ferrari Dino
wt (size): 2.77","Car: Maserati Bora
wt (size): 3.57","Car: Volvo 142E
wt (size): 2.78"],"mode":"markers","size":[2.62,2.875,2.32,3.215,3.44,3.46,3.57,3.19,3.15,3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.2,1.615,1.835,2.465,3.52,3.435,3.84,3.845,1.935,2.14,1.513,3.17,2.77,3.57,2.78],"marker":{"colorbar":{"title":"wt"},"colorscale":[[0,"#440154"],[0.111111111111111,"#482878"],[0.222222222222222,"#3E4A89"],[0.333333333333333,"#31688E"],[0.444444444444444,"#26828E"],[0.555555555555556,"#1F9E89"],[0.666666666666667,"#35B779"],[0.777777777777778,"#6DCD59"],[0.888888888888889,"#B4DE2C"],[1,"#FDE725"]],"color":[2.62,2.875,2.32,3.215,3.44,3.46,3.57,3.19,3.15,3.44,3.44,4.07,3.73,3.78,5.25,5.424,5.345,2.2,1.615,1.835,2.465,3.52,3.435,3.84,3.845,1.935,2.14,1.513,3.17,2.77,3.57,2.78],"size":[1374.23265210972,1507.98430336467,1216.87776828036,1686.31983837128,1804.3360012433,1814.82632683192,1872.52311756935,1673.2069313855,1652.22628020825,1804.3360012433,1804.3360012433,2134.78125728495,1956.44572227834,1982.6715362499,2753.71046701375,2844.97629963478,2803.53951355972,1153.93581474862,847.093791281374,962.487372756236,1292.93262879789,1846.29730359779,1801.71341984614,2014.14251301577,2016.76509441293,1014.93900069936,1122.46483798275,793.593130779392,1662.71660579688,1452.9100940244,1872.52311756935,1458.15525681871],"sizemode":"area"}}],"layout":{"xaxis":{"title":"wt"},"yaxis":{"title":"mpg"},"hovermode":"closest","margin":{"b":40,"l":60,"t":25,"r":10}},"url":null,"width":null,"height":null,"source":"A","config":{"modeBarButtonsToRemove":["sendDataToCloud"]},"base_url":"https://plot.ly"},"evals":[],"jsHooks":[]}</script>

Spell Check

Spelling may not be our strong suit and as a result, we may need to check the spelling in our document. There are two ways to check the spelling:

  1. Click on the "ABC check mark" to the left of the magnifying glass button in RStudio.
  2. Use the aspell() function from the utils package. However, it would be desirable to skip code chunks. The aspell() function can take a filter function to skip certain lines in the files and can be used with the function knit_filter() which was designed to skip over code chunks in a file.

Knitr Themes

The knitr syntax theme can be adjusted or completely customized. If you do not prefer the default themes, use the object knit_theme to change it. There are 80 themes contained within knitr and we can view the names of them by knit_theme$get().

What are the first 30 knitr themes?

head(knit_theme$get(), 30)
##  [1] "acid"          "aiseered"      "andes"         "anotherdark"  
##  [5] "autumn"        "baycomb"       "bclear"        "biogoo"       
##  [9] "bipolar"       "blacknblue"    "bluegreen"     "breeze"       
## [13] "bright"        "camo"          "candy"         "clarity"      
## [17] "dante"         "darkblue"      "darkbone"      "darkness"     
## [21] "darkslategray" "darkspectrum"  "default"       "denim"        
## [25] "dusk"          "earendel"      "easter"        "edit-anjuta"  
## [29] "edit-eclipse"  "edit-emacs"

We can use knit_theme$set() to set the theme. For example, to set the theme to fruit we could run the following code:

knit_theme$set("fruit")

Here's the link to find your favorite theme of all the 80 knitr highlight themes.

Other Programming Languages

While knitr must be run within the R environment, it also supports many other programming languages including:

  • Python
  • Ruby
  • Haskell
  • awk/gawk
  • sed
  • shell scripts
  • Perl
  • SAS
  • TikZ
  • Graphviz
  • C++
  • And others...

However, we have to install the corresponding software package in advance to use an engine.

Enter the engine function in Knitr. This function allows the user to specify the language for a chunk.

  • engine = "bash" will load in bash and allow the user to write shell scripts within that code chunk.

Appearance

There are many options that we can control within our .Rmd document. This section helps to introduce and explore some of these customization options for HTML documents.

Code Folding

As you may have noticed, each one of the code chunks in this document has an interactive button. This is controlled in the YAML header and is new as of RMarkdown v2.

When the knitr code chunk option echo = TRUE is specified (default = TRUE) the R code within the chunk will be included in the output document. However, there may be times in which a user would like to exclude code entirely (echo = FALSE)

-code_folding:
- code_folding: hide: Can include R code but have it hidden by default.
- code_folding: show: Shows R code and then the reader can click the button to hide that chunk if they wish.

output: html_document
    code_folding: show

Table of Contents

A table of contents can be added to the rendered document by using the toc option in the YAML header.

Options Include:

  • toc: Whether to include to the table of contents:
    • toc: true: Includes a table of contents
    • Default:toc: false: Does not include a table of contents
  • toc_depth:: How many levels within the table of contents should be included in the table of contents?
    • Default: doc_depth: 3 will include headers with ###.
  • number_sections: Add section numbers to the headers. For example, this document has number_sections: true
    • Default: number_sections: false
    • Note: With each # there will be a decimal point added to all headers.
  • toc_float:
    • 2 other optional parameters within toc_float:
      • collapsed: Controls if the table of contents appears with only the top level. It will expand with the cursor over it.
        • Default: collapsed: TRUE
      • smooth_scroll: Controls whether the page scrolls are animated when the items within the table of contents are navigated to via clicking on them.
        • Default: smooth_scroll: true

For example:

output:
  html_document:
    toc: true
    toc_depth: 2
---

Challenge: Make the YAML header for an HTML document that includes:

  • Table of contents
  • Float the table of contents
  • Section headers with two hashtags (##)
  • Numbered sections
  • No smooth scrolling

Themes

RMarkdown has several options that control the appearance of HTML documents. Some arguments to choose from are:

  • theme
  • highlight
  • smart

The HTML output themes are drawn from the Bootswatch library. Valid HTML themes include the following:

  • cerulean, cosmo,flatly, journal, readable,spacelab, and united.
    • For example, the theme of this page is readable.
  • Pass null for no theme (in this case you can use the css parameter to add your own styles).

Highlight specifies the syntax highlighting style. Supported styles include the following:

  • default, espresso, haddock, kate, monochrome, pygments, tango, textmate, and zenburn.
  • Pass null to prevent syntax highlighting.

Smart indicates whether to produce typographically correct output, converting straight quotes to curly quotes, --- to em-dashes, -- to en-dashes, and ... to ellipses. Smart is enabled by default.

For example:

---
output:
  html_document:
    theme: slate
    highlight: tango
---

If you felt inclined, you could also produce and use your own theme. If you did so, the output section of your YAML header would like like:

output:
  html_document:
    css: styles.css

If you wanted to go the extra mile and write your own theme in addition to highlight, the YAML header would look like:

---
output:
  html_document:
    theme: null
    highlight: null
    css: styles.css
---

Here's a link to learn more about the Appearance and Style in HTML output.


Caching

Problem: Some code chunks take a long time to run and may not be updated very often.

Solution: Caching! If a code chunk has not been modified since the last rendering of the document, the old results will be directly used instead of re-running the chunk.

Very simple solution: Exit knitting early -> if the rest of the document does not need to be rendered, simply put the knit_exit() and the rest of the document will be ignored. Therefore, results from all previous text and code chunks will be returned.

lazy-loading is when an object will not be loaded into memory until it is used. A "promise" is instead created, which is computationally easy (to learn more run?promise).

Therefore, when rendering the document, cached chunks are skipped and the output created previously from these chunks will be (lazy-) loaded from the cache folder.

However, if a single change to the chunk occurs (even a white space counts!) then knitr will acknowledge this change and will actually run the chunk when the document is rendered.

The user can also set the path for where the cached files should be by using cache.path

  • Default:cache.path = "file_name_cache/"
    • For example: Check out the github repo for this lesson previously cached chunks are in the RMarkdown_Lesson_cache directory.

Some issues with caching:

  1. R is updated every few months. R.version.string
  2. External files that knitr does not track changes, may be updated and results will need to be re-run.
  3. Sometimes a cached chunk might rely on objects from other cached chunk. This can be a serious problem - so be careful! Therefore, we should cover chunk dependencies!

Chunk Dependencies

Manual

We can manually specify whether chunks depend on each other.

  • dependson specifies which other chunks the current chunk relies on by setting a vector of chunk labels.
    • Examples with integers:
      • depesndson = 1: Chunk relies on first Chunk
      • dependson = c(6,8): Chunk relies on 6^th^ and 8^th^ chunks
      • dependson = -1: Chunk relies on previous chunk.
      • dependson = c(-1, -2): Chunk relies on the two previous chunks.
      • Note: When dependson takes on an integer value, it cannot make a chunk depend on later chunks - only previous chunks. Therefore, it may be useful to use the chunk names or vectors of chunk names.
    • Examples with chunk names:
      • dependson = c("Chunk-1", "Chunk-2", "Chunk-3")
      • dependson = c("data-generation", "data-transforamtion")
  • As a result, each time one of the cached chunks "Chunk-1", "Chunk-2", and "Chunk-3" are re-built, the current chunk will lose its cache and also be rebuilt!

Automatic

Enter: autodep chunk option and function dep_auto()

  • autodep and dep_auto() state that if any objects in the current chunk are created in previous chunks, then the current chunk depends on a previous chunk.

For a more conservative approach, enter dep_prev().

  • dep_prev states that a cached chunk will depend on all of its previous chunks. Therefore, whenever previous chunks are updated, all the later chunks will also be updated.

Knitr only tracks changes with cached chunks, not in uncached chunks! Fortunately, knitr will give a warning when it sees a dependency on an uncached chunk.

Load Cache Manually

Suppose you calculated a value z in a chunk towards the end of the document, however, you would like to use z in an earlier chunk. This is impossible because knitr compiles the document in a linear fashion and it cannot use objects created in the future.

Enter: load_cache, which takes a chunk label to find the cache database

load_cache(label, object, notfound = "NOT AVAILABLE", 
  path = opts_chunk$get("cache.path"), lazy = TRUE)

Therefore, if you use z in an inline R expression, it will output NOT AVAILABLE and since you have specified notfound = "NOT AVAILALBE" it will go back at the end and replace it with the value of z.

So cool!

Side Effects

A side effect refers to a state change that occurs outside of a function that is not the returned value.

  • par() and options() are side effects in the sense that they are not cached.
  • Set all the global options in the first chunk and never cache that chunk.

We need to be careful with the chunk options to be sure that results from cached chunks are up-to-date.

We can also turn off lazy-loading by setting the cache.lazy = FALSE.

Bibliography

It's also possible to include a bibliography file in the YAML header. Bibliography formats that are readable by Pandoc include the following:

| Format | File extension | |-----+-------| | MODS | .mods | | BibLaTeX | .bib | | BibTeX | .bibtex | | RIS | .ris | | EndNote | .enl | | EndNote XML | .xml | | ISI | .wos | | MEDLINE | .medline | | Copac | .copac | | JSON citeproc | .json |

To create a bibliography in RMarkdown, two files are needed:

  1. A bibliography file with the information about each reference.
  2. A citation style language (CSL) to describe how to format the reference

An example YAML header with a bibliography and a citation style language (CSL) file:

output: html_document
bibliography: bibliography.bib
csl: nature.csl

Check out the very helpful webpage by the R Core team on bibliographies and citations.

If you would like to cite R packages, knitr even includes a function called write_bib() that creates a .bib entries for R packages. It will even write it to a file!

write_bib(file = "r-packages.bib") # will write all packages  
write_bib(c("knitr", "ggplot2"), file = "r-packages2.bib") # Only writes knitr and ggplot2 packages

Placement

Automatically the bibliography will be placed at the end of the document. Therefore, you should finish your .Rmd document with # References so the bibliography comes after the header for the bibliography.

final words...

# References

Citation Styles

Citation Sylte Language (CSL) is an XML-based language that identifies the format of citations and bibliographies. Reference management programs such as Zotero, Mendeley and Papers all use CSL.

Search for your favorite journal and CSL in the Zotero Style Repository, which currently has 8,152 CSLs. Is there a style that you're looking for that is not there?

output: html_document
bibliography: bibliography.bib
csl: nature.csl

In the github repo for this workshop I have included nature.csl and the-isme-journal.csl to play around with. Otherwise, download a citation from the Zotero Style Repository!

Citations

Citations go inside square brackets [ ]and are separated by semicolons ;. Each citation must have a key, composed of @ + the citation identifier from the database, and may optionally have a prefix, a locator, and a suffix. To check what the citation key is for a reference, take a look at the .bib file. Here in this file, you can also change key for each reference. However, be careful that each ID is unique!

Here are some examples with their code:

  • Microbes control Earth's biogeochemical cycles [@Falkowski2008].
    • Code: Microbes contorl Earth's biogeochemical cycles [@Falkowski2008].
  • I love making beautiful plots with ggplot2 [@R-ggplot2]
    • Code: I love making beautiful plots with ggplot2 [@R-ggplot2]
  • Dr. Yuhui Xie's book about Dynamic Documents [@Xie2015] inspired me to host this workshop.
    • Code: Dr. Yuhui Xie's book about Dynamic Documents [@Xie2015] inspired me to host this workshop.
  • A great article in Science regarding biogeography of microbes asks readers to imagine their Alice in Wonderland to shrink down to understand the microbial world [@Green2008].
    • Code: A great article in *Science* regarding biogeography of microbes asks readers to imagine they are Alice in Wonderland to and shrink down to understand the microbial world [@Green2008].

The coolest thing is that the only references added to the document will be the ones that you have cited!

Publishing on RPubs

Once you make a beautiful dynamic document you may wish to share it with others. One option to share it with the world is to host it on RPubs. With RStudio, this makes is very easy! Do the following:

  1. Create your awesome .Rmd document.
  2. Click the button to render your HTML document to be published.
  3. In the top right corner of the preview window, click the publish button and follow the directions.
    • Note: You will need to create an RPubs profile.
  4. Once you have a profile you can choose the following:

Updating RPubs

If you make some changes to your document it is very easy to update the webpage. Once you have rendered your edited document click the button on the top right corner of the preview window. The edited document will be in the same URL as the original document.

Thank you

Thank you for participating in this tutorial.

If you have updates you would like to make to the lesson, please send me a pull request. Alternatively, if you have any questions, please e-mail me.

Marian Schmidt
E-mail: marschmi at umich.edu
Twitter @micro_marian

Good luck with your pursuit of dynamic documents!

Session Info

devtools::session_info()
## Session info --------------------------------------------------------------
##  setting  value                       
##  version  R version 3.3.0 (2016-05-03)
##  system   x86_64, darwin13.4.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       America/Detroit             
##  date     2016-05-12
## Packages ------------------------------------------------------------------
##  package     * version date       source        
##  assertthat    0.1     2013-12-06 CRAN (R 3.3.0)
##  base64enc     0.1-3   2015-07-28 CRAN (R 3.3.0)
##  colorspace    1.2-6   2015-03-11 CRAN (R 3.3.0)
##  DBI           0.4     2016-05-02 CRAN (R 3.3.0)
##  devtools      1.11.1  2016-04-21 CRAN (R 3.3.0)
##  digest        0.6.9   2016-01-08 CRAN (R 3.3.0)
##  dplyr       * 0.4.3   2015-09-01 CRAN (R 3.3.0)
##  DT          * 0.1     2015-06-09 CRAN (R 3.3.0)
##  evaluate      0.9     2016-04-29 CRAN (R 3.3.0)
##  formatR       1.3     2016-03-05 CRAN (R 3.3.0)
##  ggplot2     * 2.1.0   2016-03-01 CRAN (R 3.3.0)
##  gridExtra     2.2.1   2016-02-29 CRAN (R 3.3.0)
##  gtable        0.2.0   2016-02-26 CRAN (R 3.3.0)
##  highr         0.5.1   2015-09-18 CRAN (R 3.3.0)
##  htmltools     0.3.5   2016-03-21 CRAN (R 3.3.0)
##  htmlwidgets   0.6     2016-02-25 CRAN (R 3.3.0)
##  httr          1.1.0   2016-01-28 CRAN (R 3.3.0)
##  jsonlite      0.9.19  2015-11-28 CRAN (R 3.3.0)
##  knitr       * 1.13    2016-05-09 CRAN (R 3.3.0)
##  labeling      0.3     2014-08-23 CRAN (R 3.3.0)
##  magrittr      1.5     2014-11-22 CRAN (R 3.3.0)
##  memoise       1.0.0   2016-01-29 CRAN (R 3.3.0)
##  munsell       0.4.3   2016-02-13 CRAN (R 3.3.0)
##  pander      * 0.6.0   2015-11-23 CRAN (R 3.3.0)
##  plotly      * 3.4.13  2016-04-12 CRAN (R 3.3.0)
##  plyr          1.8.3   2015-06-12 CRAN (R 3.3.0)
##  R6            2.1.2   2016-01-26 CRAN (R 3.3.0)
##  Rcpp          0.12.4  2016-03-26 CRAN (R 3.3.0)
##  rmarkdown   * 0.9.6   2016-05-01 CRAN (R 3.3.0)
##  scales        0.4.0   2016-02-26 CRAN (R 3.3.0)
##  stringi       1.0-1   2015-10-22 CRAN (R 3.3.0)
##  stringr       1.0.0   2015-04-30 CRAN (R 3.3.0)
##  tidyr         0.4.1   2016-02-05 CRAN (R 3.3.0)
##  viridis       0.3.4   2016-03-12 CRAN (R 3.3.0)
##  withr         1.0.1   2016-02-04 CRAN (R 3.3.0)
##  yaml          2.1.13  2014-06-12 CRAN (R 3.3.0)

References