Rmarkdown
Rmarkdown is a way to integrate narrative text with R code and output. It’s ideal for writing your homework since it’s easy to integrate code, and output but it’s also great for ensuring your analyses are reproducible (I also use it to write the lecture slides, labs, homework and solutions).
The Rmarkdown you’ll be working with uses the data from HW#3. So, make sure you have it first:
download.file("http://st551.cwick.co.nz/data/brfss.rds",
"brfss.rds", mode = "wb")
Download the and open the Rmarkdown file lab-5-rmarkdown.Rmd. For the rest of the lab the content is in the Rmarkdown file, but to help you navigate the essential parts are also listed here.
“Knitting” the file
Start by knitting the file (the Knit icon on the toolbar, or Cmd/Crtl + Shift + K). The code chunks in the file are run in a fresh session of R, output and figures are generated, and the whole document is produced as a .pdf.
(You may be asked to install some additional packages before knitting will work).
Code chunks
Lines of the file that begin with: ```{r}
designate the beginning of a code chunk. Until the closing ```
, everything inside the chunk is interpreted as R code. When knit, this code is run, and the output and figures are captured.
Whether the code itself, the results or the figures are displayed in the final document depends in the chunk options that follow the r
in ```{r}
. For example ```{r, echo = FALSE}
, prevents the code from displaying in the document, ```{r, results = "hide"}
prevents the results from being included in the document. You can find all the possible options at the knitr chunk options page.
Markdown
The text in the document that isn’t in a code chunk is interpreted as Markdown, a very simple system for document markup. You can specify section headings, bold or italic font, bulleted or numbered lists, tables, links and images. Read more in ]Chapter 27.3 in R4DS](http://r4ds.had.co.nz/r-markdown.html#text-formatting-with-markdown).
Math
Rmarkdown also has the ability to typeset equations using LaTex syntax. Put an inline equation between single dollar signs, $
, e.g. $\overline{Y} = \sum_{i = 1}^{n}$
, and displayed equations (i.e. centered on a new line) with $$
, e.g.
$$
\overline{Y} = \sum_{i = 1}^{n} \dot \sim N(0, 1)
$$
Inline code
You can also put evaluated R code inline in markdown sections. This is very useful for including a calculated number in a sentence. To do so, start with `r
, write your R code then finish with `
. For example, \(\pi\) to 3 .d.p is `r round(pi, 3)`
Which displays as : “3.142”
Tables
You can also insert nicely formatted tables in Rmarkdown. I find the pander
package quite good, because it results in a markdown table which can then be easily converted to HTML, PDF or Word. You just need to make sure you set the chunk option results
to asis
.
For example,
```{r, results = "asis"}
library(pander)
pander(head(mtcars))
```
Results in the table:
mpg | cyl | disp | hp | drat | wt | qsec | vs | |
---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21 | 6 | 160 | 110 | 3.9 | 2.62 | 16.46 | 0 |
Mazda RX4 Wag | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.32 | 18.61 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.44 | 17.02 | 0 |
Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.46 | 20.22 | 1 |
am | gear | carb | |
---|---|---|---|
Mazda RX4 | 1 | 4 | 4 |
Mazda RX4 Wag | 1 | 4 | 4 |
Datsun 710 | 1 | 4 | 1 |
Hornet 4 Drive | 0 | 3 | 1 |
Hornet Sportabout | 0 | 3 | 2 |
Valiant | 0 | 3 | 1 |
Looking ahead
Starting with HW #5 I will expect your submissions to be generated from Rmarkdown, you will submit the compiled .pdf and the source .Rmd document.
Learn more about Rmarkdown at: http://rmarkdown.rstudio.com/lesson-1.html
Also check out the Rmarkdown cheatsheet.
A couple of practical hints:
- Compile errors are often cryptic but often come from R code errors, start by starting a fresh session of R and running all the code in the chunks. Fix the errors in the code before looking for errors elsewhere.
- Compile early and often.
- Every time you compile, all code is run in a fresh session. Code that takes a long time to run (i.e. a big simulation) will slow this down. Consider moving the simulation code to a separate file, and saving the output there (with e.g.
write_rds()
from thereadr
package), and reading the output into your Rmarkdown file (with e.g.read_rds()
from thereadr
package).