-
Notifications
You must be signed in to change notification settings - Fork 0
/
RR.Rmd
298 lines (194 loc) · 6.44 KB
/
RR.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
% A Shot at Reproducible Data Analysis
% Toni Verbeiren
% 13/3/2015
# Introduction
In this talk/document/presentation I showcase some of the possibilities that a combination of _tools_ provides:
* [Markdown](http://daringfireball.net/projects/markdown/)
* [RMarkdown](http://rmarkdown.rstudio.com/)
* [Knitr](http://yihui.name/knitr/)
* [Pandoc](http://johnmacfarlane.net/pandoc/)
* [Reveal.js](http://lab.hakim.se/reveal-js/#/)
* [Latex](http://www.latex-project.org/)
- - -
In order to make sure things look good from the first start, you might check out some additional projects and files:
* Bootstrap template for Pandoc:
<https://github.com/tonyblundell/pandoc-bootstrap-template>
* Alternative LaTeX templates:
<https://github.com/kjhealy/latex-custom-kjh>
* Alternative Pandoc template:
<https://github.com/kjhealy/pandoc-templates>
* Non-official KU Leuven templates:
<https://github.com/exporl/kuleuven-templates>
- - -
# Idea
- - -
## Workflow
1. Write data generation, data manipulation and discussion in **one text file**.
* Syntax for text is Markdown.
* Code lines start with `tab` or delimited by `` ``` ``
* Call this file `file.Rmd`, even if it includes more than `R` code.
2. Call `knitr` on the `.Rmd` file in order to **execute** the code blocks and **include** the output of the code in one file. The output is a `.md` file.
3. Call `Pandoc` on the file, given suitable options (see below). `Pandoc` is responsible for translating the `.md` file to **any format** you want.
- - -
## RMarkdown format
The `.Rmd` source of this report looks like this (50 lines):
```{r, results="markup", comment=""}
text <- readLines("RR.Rmd",encoding="UTF-8")
tail(head(text, 70),50)
```
- - -
## Markdown format
The `.md` source of this report looks like this (50 lines):
```{r, results="markup", comment=""}
text <- readLines("RR.md",encoding="UTF-8")
tail(head(text, 70),50)
```
Conversion is done using `knitr`.
- - -
## Pandoc
A simple and a more involved example of running `Pandoc`:
```shell
pandoc file.md -o file.docx
pandoc file.md -o file.html \
-t html5 \
--template template.html \
--css template.css \
--highlight-style=tango --mathjax \
--toc --toc-depth 2
```
Dust off your `Makefile` skills!
- - -
# Some Examples
- - -
## Simple example
The first example is in `R`. Let's say I want to plot a function
$$ f(x) = \frac{\log(x^2+x+1)}{2x} $$
We first define $x$ and the function value $y$ (in doing so we have used some inline equations as well):
```{r}
x <- seq(from=-5,to=10,by=.01)
y <- (log(x*x + x + 1))/(2*x)
```
- - -
Then we can plot the function. We use the `ggplot2` package.
```{r chunk, fig.cap="Plot of the very special function defined above.",echo=TRUE,message=FALSE}
library(ggplot2)
qplot(x,y,geom="line")
```
See the figure for the result.
- - -
## Working with data
Let us take a look at a dataset that comes with `R`, `mtcars`:
```{r}
summary(mtcars)
```
- - -
Now the fun starts. Let's fit a model relates how many Miles/Gallon are consumed, given a weight.
```{r}
model <- lm(mpg ~ wt, data=mtcars)
summary(model)
```
- - -
This is verbatim output, we can use some `R` package magic to get proper tables as output as well using the `pander` package:
```{r, results="asis", message=FALSE}
library(pander)
pander(model)
```
- - -
We can also plot this information using the code below.
```{r, fig.cap="A scatterplot of the fuel consumption versus the weight of the car, along with the results of a linear regression. See the text for more information."}
qplot(x=wt, y=mpg, data=mtcars, xlab="Weight (lb/1000)", ylab="Miles per Gallon",
geom=c("point","smooth"), method="lm")
```
- - -
## Scraping the web
This script parses the Wikipedia page with Belgian Beers in order to get the data out.
It then does some cleaning up and converts the data to different formats.
The result can be stored in a file, but just display the first 10 rows.
- - -
```{r, message=FALSE}
library(XML)
rawBeers <- readHTMLTable(doc="http://nl.wikipedia.org/wiki/Lijst_van_Belgische_bieren")
beers <- NULL
# The first table is not relevant, the rest is:
for (i in seq(2,28)) {
beers <- rbind(beers,rawBeers[[i]])
}
# Remove the percentage sign and convert to numbers:
beers$Percentagealcohol <- gsub("%","",beers$Percentagealcohol)
beers$Percentagealcohol <- gsub(",",".",beers$Percentagealcohol)
beers$Percentagealcohol <-as.numeric(beers$Percentagealcohol)
# A few entries do not have a percentage entry
nas <- length(beers[is.na(beers$Percentagealcohol),])
```
- - -
The number of entries without percentage entry is: `r nas`.
We use `pander` again for displaying the top-10 of beers with the highest amount of alcohol:
- - -
```{r, results="asis"}
pander(
head(
beers[order(beers$Percentagealcohol,decreasing=TRUE),
c("Merk","Percentagealcohol")],
10)
)
```
- - -
# Different languages
- - -
## Python
```{r , cache=TRUE, engine="python"}
import pprint
pprint.pprint(zip(('Byte', 'KByte', 'MByte', 'GByte', 'TByte'),
(1 << 10*i for i in xrange(5))))
```
- - -
## Scala
```{r , cache=TRUE, engine="scala"}
val collection = for {i <- 1 to 10} yield {i}
val mapped = collection map (x => x*x)
val reduced = mapped reduce (_ + _)
println(reduced)
```
- - -
# Sweave
- - -
`knitr` can handle sweave documents as well.
```R
library(knitr)
Sweave2knitr('dummy.Rnw')
knit('dummy-knitr.Rnw')
```
- - -
Or, just write in RMarkdown:
```script
Rscript -e 'library(knitr); knit("rmarkdown-version.Rmd")
pandoc rmarkdown-version.md -o rmarkdown-version.pdf --toc
```
Text (and code) can be translated using `Pandoc`
- - -
![Side-by-side view of the same text/code in RMarkdown and Sweave](figure/SweaveVsRmd.png)
- - -
# What to use it for?
I use it for:
* Creating presentations (`reveal.js`)
* Writing reports (including code)
* Writing papers (just text)
* Making coffee
- - -
# How to use it?
- - -
## RStudio
![Screenshot of (part of) RStudio](figure/RStudio-Screenshot.png)
- - -
## _your favourite editor here_
![Screenshot of Sublime Editor with Markdown mode](figure/Sublime-Screenshot.png)
- - -
# Additional pointers
* Markdown to `Reveal.js`:
<http://tverbeiren.github.io/BigDataBe-Spark/#/>
* Markdown and `Pandoc` for writing a paper:
<http://homes.esat.kuleuven.be/~bioiuser/blog/?p=243>
* Markdown and `Pandoc` for lecture notes:
<https://bitbucket.org/tverbeiren/i0u19a>
* You can find everything I showed here at:
<http://github.io/tverbeiren/ReproducibleDataAnalysis/>