Reproducibility
Reproducible research
is a hot topic these days, but you want nearly
anything you do in R to be reproducible. Even homework. If you cannot
reproduce what you did, then how can anyone — even you yourself —
have any confidence that what you did is correct?
The way you make an R analysis reproducible is to
- just make an R file that that reproduces it and then do
R CMD BATCH --vanilla <filename>
where<filename>
is the name of the plain text file containing your R code. Conventionally, this file has suffix.R
, for example,foo.R
.The R source and any results printed by R go in a file with the same name except that the suffix
.R
is changed to.Rout
. Any plots go (by default) into a file namedRplots.pdf
.
But it is much nicer to make a nice text document in some format like PDF or HTML that includes R not by cut-and-paste but rather by always recalculating from the R source whenever the document is produced.
Then the document (1) looks nice but (2) is still fully reproducible.
Now that reproducibility is such a hot topic, people are becomimg more skeptical of any document that is not reproducible. If your work isn't reproducible, it doesn't have much credibility. How can anyone trust that your computations are correct when big corporations with highly trained software engineers cannot produce bug-free code? Why are you so much better than everybody else?
Your humble instructor has made enough bugs so that he does not expect anyone to take any analysis he does on faith. Every publication he has had in the last ten years that involves computing has been backed up by one or more technical reports that have completely reproducible calculations. The main R tools for this are
-
Sweave
the oldest. -
knitr
newer and purportedly better (certainly fancier). -
Rmarkdown
newer and purportedly simpler (although doing anything really complicated is actually harder or impossible).
Output Files
Her are some example files that illustrate R code, plots, and tables.
- PDF output from
Sweave
foo.pdf. - PDF output from
knitr
bar.pdf. - PDF output from
Rmarkdown
baz.pdf. - HTML output from
Rmarkdown
baz.html.