By: Marian L. Schmidt, @micro_marian
, marschmi at umich.edu
May 11th, 2016
This workshop was hosted on May 11th, 2016 and has 2 supplemental materials available:
- Class notes can be found on the workshop etherpad.
- 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:
- University of California, Davis
- Simon Fraser University
- Ontario Institute for Cancer Research
The Github repository for this lesson can be found here.
Before working through the workshop materials, please do the following in preparation:
- Open up RStudio.
- 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
- 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 thesession_info()
function within the devtools package rather than thesessionInfo()
function within the utils package. Runningdevtools::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.
- 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
- 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!
I could not have made this tutorial without these amazing resources:
- The RMarkdown website hosted by RStudio.
- 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!!
- Dr. Karl Broman's "Knitr in a Knutshell".
- Cheatsheets released by RStudio.
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:
- Parse the source document and separate code from narratives.
- Execute source code and return results.
- Mix results from the source code with the original narratives.
So that leaves 2 steps for us which includes writing:
- Analysis code
- 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
\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
(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 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:
Reproducible ResearchReproducible research is one possible product of dynamic documents, however, it is not guaranteed! Good practices for reproducible research include:
To read more about reproducibility and data management check out Vince Buffalo's Book[@Buffalo2015]. MarkdownTo 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:
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! RMarkdownRMarkdown 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 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!
Simple WorkflowBriefly, to make a report:
Overview of the steps RMarkdown takes to get to the rendered document:
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
|
---|
Description | Code | Examples |
---|---|---|
Greek letters |
$\alpha$ $\beta$ $\gamma$ $\rho$ $\sigma$ $\delta$ $\epsilon$ $mu$
|
|
Binary operators |
$\times$ $\otimes$ $\oplus$ $\cup$ $\cap$
|
|
Relation operators |
$< >$ $\subset$ $\supset$ $\subseteq$ $\supseteq$
|
|
Others |
$\int$ $\oint$ $\sum$ $\prod$
|
|
Challenge: Try to mimic the output of the following:
-
Today, I am bold and am learning RMarkdown.
-
honey is very sweet.
-
YAS!!!^!!!^
-
R^2^ values are informative!
-
$R^{2}$ describe the variance explained in the model. -
I do not know RMarkdownToday I learned RMarkdown
-
Output from the following:
# RMarkdown
## R
### Knitr
#### Pandoc
##### HTML
-
$\sqrt{b^2 - 4ac}$ -
$$\sqrt{b^2 - 4ac}$$ -
$X_{i,j}$
- Today is the day I will make a dynamic document!
- The following list:
Chocolate Chip cookie Recipe
- butter
- sugar
- A mix of brown & white sugar makes it more delicious
- mix with butter before you add the eggs
- A mix of brown & white sugar makes it more delicious
- eggs
- vanilla
- Mix dry ingredients:
- flour, salt, baking soda
- 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.
-
Inline Code: Brief code that takes place during the written part of the document.
-
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:
`�``{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
- Identifies code including chunks and inline
- Evaluates all the code and returns the results
- 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:
- Always name/label your code chunks!
- 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.
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.
-
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
andfig.height
-
Default:
fig.width = 7
,fig.height = 7
-
Default:
-
fig.align
: How to align the figure-
Options include:
"left"
,"right"
, and"center"
-
Options include:
-
fig.path
: A file path to the directory to where knitr should store the graphic output created by the chunk.-
Default:
'figure/'
-
Default:
- 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))
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))
\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) 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 CheckSpelling 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:
Knitr ThemesThe knitr syntax theme can be adjusted or completely customized. If you do not prefer the default themes, use the object What are the first 30 head(knit_theme$get(), 30)
We can use knit_theme$set("fruit") Here's the link to find your favorite theme of all the 80 knitr highlight themes. Other Programming LanguagesWhile knitr must be run within the R environment, it also supports many other programming languages including:
However, we have to install the corresponding software package in advance to use an engine. Enter the
AppearanceThere are many options that we can control within our Code FoldingAs 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 -
Table of ContentsA table of contents can be added to the rendered document by using the Options Include:
For example:
ThemesRMarkdown has several options that control the appearance of HTML documents. Some arguments to choose from are:
The HTML output themes are drawn from the Bootswatch library. Valid HTML themes include the following:
Highlight specifies the syntax highlighting style. Supported styles include the following:
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:
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:
If you wanted to go the extra mile and write your own theme in addition to highlight, the YAML header would look like:
Here's a link to learn more about the Appearance and Style in HTML output. CachingProblem: 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 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 Therefore, when rendering the document, cached chunks are skipped and the output created previously from these chunks will be (lazy-) loaded from the However, if a single change to the chunk occurs (even a white space counts!) then The user can also set the path for where the cached files should be by using
Some issues with caching:
Chunk DependenciesManualWe can manually specify whether chunks depend on each other.
AutomaticEnter:
For a more conservative approach, enter
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 ManuallySuppose 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:
Therefore, if you use z in an inline R expression, it will output So cool! Side EffectsA side effect refers to a state change that occurs outside of a function that is not the returned value.
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 BibliographyIt'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:
An example YAML header with a bibliography and a citation style language (CSL) file:
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(file = "r-packages.bib") # will write all packages
write_bib(c("knitr", "ggplot2"), file = "r-packages2.bib") # Only writes knitr and ggplot2 packages PlacementAutomatically the bibliography will be placed at the end of the document. Therefore, you should finish your
Citation StylesCitation 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?
In the github repo for this workshop I have included CitationsCitations go inside square brackets Here are some examples with their code:
The coolest thing is that the only references added to the document will be the ones that you have cited! Publishing on RPubsOnce 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:
Updating RPubsIf 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 youThank 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 Good luck with your pursuit of dynamic documents! Session Infodevtools::session_info()
References |
---|