14 Advanced Documentation
Overview
The “spin” functionality in knitr for producing html from R script files (.R files) is very useful, and nearly perfrect for homework. But what do you do if you want to create more polished reports - say for you supervisor at work? Here we’ll explore the R Markdown document language that is part of knitr.
Objectives
Upon completion of this lesson, you should be able to:
- Create R Markdown documents and compile them using knitr
- Use chunk options to control size of figures and display of code
- Use the “projects” feature in RStudio to manage multiple projects in R
- Create basic pdf files using LaTeX with knitr
Data and R Code Files
The R code file and data files for this lesson can be found on the Essential R - Notes on learning R page.
14.1 Why You Might Want to Use R Markdown
In this video we’ll explore some of the benefits of compiling documents from R Markdown compared to compiling them from .R files. I hope to convince you that Rmarkdown is worth the extra complexity (which is minimal).
14.2 Basic Features of R Markdown
Here we’ll look at the basic construction of an R Markdown (.rmd) file - the big idea is that R code is contianed in “chunks” within a text document.
14.3 More Features in R Markdown
Here we’ll look in more detail at chunk options in R Markdown which control how code and the output of the code is evaluated and displayed. A full listing of chunk options can be found at the knitr website.
14.4 R Markdown Output
After all this looking at how .rmd files are constructed, we’ll look at output and see how it is improved over the output from .R files.
14.5 RStudio’s Project Feature
Once you start using R you can quickly find it difficult to keep track of your R Markdown files (or .R files) and your workspaces, etc. RStudio has a nice “project” feature that makes it simple to save the state of your RStudio window and return to it later.
14.6 Knit Together R and LaTeX with RNW
I would be remiss if I didn’t at least mention that knitr also makes it possible to combine LaTeX and R code using .rnw files. If you already use LaTeX this might interest you, or if you have other reasons to learn LaTeX. I honestly find that R Markdown does everything I need (up to and including EssentialR”), but your mileage may vary.