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.
To work with R Markdown requires three simple steps:
knitr package: install.packages("knitr")To run the basic working example:
set.seed(1234)
library(ggplot2)
library(lattice)
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
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.
Here is a basic plot using base graphics:
```r
plot(x)
```

plot(x)
Also, unlike traditional Sweave, you can include multiple plots in one code chunk:
```r
boxplot(1:10 ~ rep(1:2, 5))
```

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

boxplot(1:10 ~ rep(1:2, 5))
plot(x, y)
ggplot2 plotGgplot2 plots work well:
qplot(x, y, data = df)
lattice plotAs do lattice plots:
xyplot(y ~ x)
We can also embed maps, for example:
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
| 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 |
We can plot a figure with defualt size:
plot(x)
The following is an example of a smaller figure using fig.width=3 and fig.height=3 options.
```r
plot(x)
```

plot(x)
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)
}
RStudio includes a Markdown quick reference button that covers some basic presentation options
Simple dot points:
and numeric dot points:
and nested dot points:
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 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 |
Here's an example image, using ![image]:

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

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.
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,
R Markdown is the future.
The RStudio team have made the whole process very user friendly.
This has been adapted from Jeromy Anglim's recent post.
Other recent posts on R markdown include those by :
knitr. He has also posted this example of R Markdown.