A Quick Example of How We Can Reproduce and Share our Output

This page presents some of the basic features of R Markdown
using knitr in Rstudio 0.96.
This combination of tools should allow a vast improvement in usability for
reproducible analysis.

This page:
(1) discusses getting started with R Markdown and knitr in Rstudio 0.96;
(2) provides a basic example of producing console output and plots using R Markdown;
(3) highlights several code chunk options such as caching and controlling how input and output is displayed;
(4) demonstrates use of standard Markdown notation as well as the extended features of formulas and tables; and
(5) discusses the implications of R Markdown.

This has been produced as an R Markdown document to show an example of how to quickly and easily publish our results on the internet.

When you click the Knit HTML button in on a Markdown document in RStudio an html page will be generated that includes both content as well as the output of any embedded R code chunks within the document.
A copy of the R Markdown can be displayed in the HTML, but most of the time it is easier to read the source and output side by side.

Getting started

To work with R Markdown requires three simple steps:

To run the basic working example:

Prepare for analyses

set.seed(1234)
library(ggplot2)
library(lattice)

Basic console output

To insert an R code chunk, you can type it manually or just press Chunks - Insert chunks or use the shortcut key. This will produce the following code chunk:

Pressing tab when inside the braces will bring up code chunk options.

The following R code chunk labelled basicconsole is as follows:

```r
x <- 1:10
y <- round(rnorm(10, x, 1), 2)
df <- data.frame(x, y)
df
```

```
##     x     y
## 1   1 -0.21
## 2   2  2.28
## 3   3  4.08
## 4   4  1.65
## 5   5  5.43
## 6   6  6.51
## 7   7  6.43
## 8   8  7.45
## 9   9  8.44
## 10 10  9.11
```

The code chunk input and output is then displayed as follows:

x <- 1:10
y <- round(rnorm(10, x, 1), 2)
df <- data.frame(x, y)
df
##     x     y
## 1   1  0.52
## 2   2  1.00
## 3   3  2.22
## 4   4  4.06
## 5   5  5.96
## 6   6  5.89
## 7   7  6.49
## 8   8  7.09
## 9   9  8.16
## 10 10 12.42

We can suppress some of the output using the message=FALSE and echo=FALSE commands within the {r} header

##     x    y
## 1   1 1.13
## 2   2 1.51
## 3   3 2.56
## 4   4 4.46
## 5   5 4.31
## 6   6 4.55
## 7   7 7.57
## 8   8 6.98
## 9   9 8.98
## 10 10 9.06

Plots

Images generated by knitr are saved in a figures folder. However, they also appear to be represented in the HTML output using a data URI scheme. This means that you can paste the HTML into a blog post or discussion forum and you don't have to worry about finding a place to store the images; they're embedded in the HTML.

Simple plot

Here is a basic plot using base graphics:

```r
plot(x)
```

![plot of chunk simpleplot](figure/simpleplot.png) 
plot(x)

plot of chunk simpleplotex

Multiple plots

Also, unlike traditional Sweave, you can include multiple plots in one code chunk:

```r
boxplot(1:10 ~ rep(1:2, 5))
```

![plot of chunk multipleplots](figure/multipleplots1.png) 

```r
plot(x, y)
```

![plot of chunk multipleplots](figure/multipleplots2.png) 
boxplot(1:10 ~ rep(1:2, 5))

plot of chunk multipleplotsex

plot(x, y)

plot of chunk multipleplotsex

ggplot2 plot

Ggplot2 plots work well:

qplot(x, y, data = df)

plot of chunk ggplot2ex

lattice plot

As do lattice plots:

xyplot(y ~ x)

plot of chunk latticeex

We can also embed maps, for example:

plot of chunk unnamed-chunk-2

R Code chunk features

Create Markdown code from R

The following code hides the command input (i.e., echo=FALSE), and outputs the content directly as code (i.e., results=asis, which is similar to results=tex in Sweave).

Here are some dot points

* The value of y[1] is 1.13
* The value of y[2] is 1.51
* The value of y[3] is 2.56

Here are some dot points

Create Markdown table code from R

x y
1 1.13
2 1.51
3 2.56
4 4.46
5 4.31
6 4.55
7 7.57
8 6.98
9 8.98
10 9.06

Control figure size

We can plot a figure with defualt size:

plot(x)

plot of chunk defaultplot

The following is an example of a smaller figure using fig.width=3 and fig.height=3 options.

```r
plot(x)
```

![plot of chunk smallplot](figure/smallplot.png) 
plot(x)

plot of chunk smallplotex

Cache analysis

Caching analyses is straightforward, using the cache=TRUE option.
A for loop has been set up for (i in 1:5000) {lm((i+1)~i)}
On the first run this takes about 8 seconds.
On subsequent runs, this code was not run.

If you want to rerun cached code chunks, just delete the contents of the cache folder

for (i in 1:5000) {
    lm((i + 1) ~ i)
}

Basic markdown functionality

RStudio includes a Markdown quick reference button that covers some basic presentation options

Dot Points

Simple dot points:

and numeric dot points:

  1. Number 1
  2. Number 2
  3. Number 3

and nested dot points:

Equations

Equations are included by using LaTeX notation and including them either between single dollar signs (inline equations) or double dollar signs (displayed equations).

There are inline equations such as \( y_i = \alpha + \beta x_i + e_i \).

And displayed formulas:

\[ \frac{1}{1+\exp(-x)} \]

knitr provides self-contained HTML code that calls a Mathjax script to display formulas.

Tables

Tables can be included using the following notation
A | B | C
--- | --- | ---
1 | Male | Blue
2 | Female | Pink

A B C
1 Male Blue
2 Female Pink

Hyperlinks

Images

Here's an example image, using ![image]:

ruddy duck

Gifs

Here's an example gif, using the same command:

image

Code

Here is Markdown R code chunk displayed as code:

```r
x <- 1:10
x
```

```
##  [1]  1  2  3  4  5  6  7  8  9 10
```

And then there's inline code such as x <- 1:10.

Quote

Let's quote some stuff:

To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,

Conclusion