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:


  1. Create R Markdown documents and compile them using knitr
  2. Use chunk options to control size of figures and display of code
  3. Use the “projects” feature in RStudio to manage multiple projects in R
  4. 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.

R logo

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).

Video - STAT 485 Lesson: 14.1

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.

Video - STAT 485 Lesson: 14.2

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.

Note! Some students have reported getting error messages when trying to use the “Knit HTML” button on the editor toolbar. If you are having problems check the CRAN mirror setting under Tools > Global Options (Mac: RStudio > Preferences) - it should be set to a mirror that is closer to your location.

Video - STAT 485 Lesson: 14.3

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.

Video - STAT 485 Lesson: 14.4

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.

Video - STAT 485 Lesson: 14.5

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.

Video - STAT 485 Lesson: 14.6