forked from DanielHadley/Example-R-Markdown-on-Github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.Rmd
275 lines (192 loc) · 9.03 KB
/
Example.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
---
title: "Example of R Markdown on Github"
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## GitHub Documents
This is an example of using R Markdown format for publishing markdown documents to GitHub. When you click the **Knit** button all R code chunks are run and a markdown file (.md) suitable for publishing to GitHub is generated. Commiting everything to a repository results in this page.
I have adapted the below from [Jeromy Anglim](https://gist.github.com/jeromyanglim) who created an [earlier example](https://gist.github.com/jeromyanglim/2716336) of R Markdown (from before it could be knit into Github markdown).
This post examines the features of [R Markdown](http://www.rstudio.org/docs/authoring/using_markdown)
using [knitr](http://yihui.name/knitr/) in Rstudio.
This combination of tools provides an exciting improvement in usability for
[reproducible analysis](http://stats.stackexchange.com/a/15006/183).
Specifically, this post
(1) discusses getting started with R Markdown and `knitr` in Rstudio;
(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 post was produced with R Markdown. The original post on which it was based can be found [here](https://gist.github.com/jeromyanglim/2716336) as a gist.
The post may be most useful if the [source code](https://raw.githubusercontent.com/DanielHadley/Example-R-Markdown-on-Github/master/Example.Rmd) and displayed [Github page](https://github.com/DanielHadley/Example-R-Markdown-on-Github/blob/master/Example.md) are viewed side by side.
<!-- more -->
## Getting started
To work with R Markdown, if necessary:
* Install [R](http://www.r-project.org/)
* Install the lastest version of [RStudio](http://rstudio.org/download/)
* Install the latest version of the `knitr` package: `install.packages("knitr")`
To run the basic working example that produced this blog post:
* Open R Studio, and go to File - New - R Markdown
* If necessary install `ggplot2` and `lattice` packages: `install.packages("ggplot2"); install.packages("lattice") `
* Paste in the contents of the [raw .Rmd](https://raw.githubusercontent.com/DanielHadley/Example-R-Markdown-on-Github/master/Example.Rmd) file
* Click Knit
## Prepare for analyses
```{r }
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. 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
```
The code chunk input and output is then displayed as follows:
```{r basicconsole}
x <- 1:10
y <- round(rnorm(10, x, 1), 2)
df <- data.frame(x, y)
df
```
## Plots
Images generated by `knitr` are saved in a files folder in the associated Github repo. Pushing them creates a .md file with all of the correct file paths.
### Simple plot
Here is a basic plot using base graphics:
```{r simpleplot}
plot(x)
```
Note that unlike traditional Sweave, there is no need to write `fig=TRUE`.
### Multiple plots
Also, unlike traditional Sweave, you can include multiple plots in one code chunk:
```{r multipleplots}
boxplot(1:10~rep(1:2,5))
plot(x, y)
```
### `ggplot2` plot
Ggplot2 plots work well:
```{r ggplot2ex}
qplot(x, y, data=df)
```
### `lattice` plot
As do lattice plots:
```{r latticeex}
xyplot(y~x)
```
Note that unlike traditional Sweave, there is no need to print lattice plots directly.
## 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).
```{r , results='asis', echo=FALSE}
cat("Here are some dot points\n\n")
cat(paste("* The value of y[", 1:3, "] is ", y[1:3], sep="", collapse="\n"))
```
```{r dotpointprint, results='asis', echo=FALSE}
cat("Here are some dot points\n\n")
cat(paste("* The value of y[", 1:3, "] is ", y[1:3], sep="", collapse="\n"))
```
### Create Markdown table code from R
```{r createtable, results='asis', echo=FALSE}
cat("x | y", "--- | ---", sep="\n")
cat(apply(df, 1, function(X) paste(X, collapse=" | ")), sep = "\n")
```
### Control output display
The folllowing code supresses display of R input commands (i.e., `echo=FALSE`)
and removes any preceding text from console output (`comment=""`; the default is `comment="##"`).
```{r echo=FALSE, comment="", echo=FALSE}
head(df)
```
I also highly reccomend reviewing the `kable` function in the `printr` package [here](http://yihui.name/printr/) for pretty printing.
### Control figure size
The following is an example of a smaller figure using `fig.width` and `fig.height` options.
```{r smallplot, fig.width=3, fig.height=3}
plot(x)
```
### Cache analysis
Caching analyses is straightforward.
Here's example code.
On the first run on my computer, this took about 10 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](http://stackoverflow.com/a/10629121/180892)
```{r , cache=TRUE}
for (i in 1:5000) {
lm((i+1)~i)
}
```
## Basic markdown functionality
For those not familiar with standard [Markdown](http://daringfireball.net/projects/markdown/), the following may be useful.
See the source code for how to produce such points. However, RStudio does include a Markdown quick reference button that adequatly covers this material.
### Dot Points
Simple dot points:
* Point 1
* Point 2
* Point 3
and numeric dot points:
1. Number 1
2. Number 2
3. Number 3
and nested dot points:
* A
* A.1
* A.2
* B
* B.1
* B.2
### Equations
```{JavaScript echo=FALSE}
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type="text/javascript">MathJax.Hub.Config({tex2jax: {processEscapes: true,
processEnvironments: false, inlineMath: [ ['$','$'] ],
displayMath: [ ['$$','$$'] ] },
asciimath2jax: {delimiters: [ ['$','$'] ] },
"HTML-CSS": {minScaleAdjust: 125 } });
</script>
```
Equations are included by using LaTeX notation and including them either between single dollar signs (inline equations) or double dollar signs (displayed equations).
If you hang around the Q&A site [CrossValidated](http://stats.stackexchange.com) you'll be familiar with this idea.
I added JavaScript from [this gist](https://gist.github.com/2716053) to get the follwoing inline equation to work: $y_i = \alpha + \beta x_i + e_i$.
### Tables
Tables can be included using the following notation
A | B | C
--- | --- | ---
1 | Male | Blue
2 | Female | Pink
### Hyperlinks
* If you like this post, you may wish to subscribe to [Jeremy's RSS feed](http://feeds.feedburner.com/jeromyanglim), or [my blog](http://danielphadley.com/).
### Images
Here's an example image:
![image from redmond barry building unimelb](http://i.imgur.com/RVNmr.jpg)
### Code
Here is Markdown R code chunk displayed as code:
```{r}
x <- 1:10
x
```
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
* R Markdown is awesome.
* The ratio of markup to content is excellent.
* For exploratory analyses, blog posts, and the like R Markdown will be a powerful productivity booster.
* For journal articles, LaTeX will presumably still be required.
* The RStudio team have made the whole process very user friendly.
* RStudio provides useful shortcut keys for compiling to HTML, and running code chunks. These shortcut keys are presented in a clear way.
* The incorporated extensions to Markdown, particularly formula and table support, are particularly useful.
* Jump-to-chunk feature facilitates navigation. It helps if your code chunks have informative names.
* Code completion on R code chunk options is really helpful. See also [chunk options documentation on the knitr website](http://yihui.name/knitr/options).
* Other recent posts on R markdown include those by :
* [Christopher Gandrud](http://christophergandrud.blogspot.com.au/2012/05/dynamic-content-with-rstudio-markdown.html)
* [Markcus Gesmann](http://lamages.blogspot.com.au/2012/05/interactive-reports-in-r-with-knitr-and.html)
* [Rstudio on R Markdown](http://rstudio.org/docs/authoring/using_markdown)
* [Yihui Xie](http://yihui.name/knitr/): I really want to thank him for developing `knitr`.
He has also posted [this example of R Markdown](https://github.com/yihui/knitr/blob/master/inst/examples/knitr-minimal.Rmd).