-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmarkdown.Rmd
540 lines (333 loc) · 11.5 KB
/
rmarkdown.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
---
output:
revealjs::revealjs_presentation:
incremental: false
self_contained: false
lib_dir: libs
center: true
css:
- "assets/theme_white.css"
transition: slide
slide_level: 1
reveal_plugins: ["notes"]
includes:
in_header: assets/header.html
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(width = 50)
```
# R Markdown
![](images/rmarkdown.png)
<aside class="notes" data-markdown>
</aside>
---
# What is<br>R Markdown? {data-background=#401F68}
<aside class="notes" data-markdown>
You may have seen the R Markdown file type when you go to make a new document in RStudio, or see documents with R code published online and have some awareness that they were made with this thing called R Markdown.
R Markdown is a way to write a document with executable R code in it.
You may want to show both the code and the output, or you may just want to use R generate output that is integrated with other text or content, leaving out the R code.
</aside>
---
[Gallery](http://rmarkdown.rstudio.com/gallery.html)
[RPubs](https://rpubs.com/)
[Formatted Templates](https://github.com/juba/rmdformats)
[Research Computing Workshop](https://github.com/nuitrcs/rworkshops) [Files](https://nuitrcs.github.io/ggplotworkshop/ggplot.html)
---
# Why? {data-background=#401F68}
---
## Reproducible Workflows
---
[video](https://www.youtube.com/embed/s3JldKoA0zw?showinfo=0&rel=0)
---
## Version Control
![version control](images/versioncontrol.gif)
<div class="caption">[Piled Higher and Deeper by Jorge Cham](http://phdcomics.com/comics/archive_print.php?comicid=1531)</div>
<aside class="notes" data-markdown>
R Markdown documents are plain text, so you can easily track changes with git or other version control systems.
Previews in GitHub, or post html there
</aside>
---
## Multiple Versions
![output](images/RMarkdownOutputFormats.png)
<div class="caption">[Source](http://rmarkdown.rstudio.com/authoring_quick_tour.html)</div>
<aside class="notes" data-markdown>
HTML for the web, pdf for distribution, Word for your coauthor to work with.
</aside>
---
## Easy for you and your Audience
<img src="https://media.giphy.com/media/3o7btNa0RUYa5E7iiQ/giphy.gif" height=300px>
<aside class="notes" data-markdown>
Make your code and its output accessible to both people who may know R and people who don't. You have the option to hide your code entirely (or use code folding).
I've had a number of students tell me that they use R or want to use R, but their advisor doesn't. They don't want to send their advisor R code, but it can take a lot of time to reformat output, write it up, etc. With R markdown, sharing is easier.
In my position, I may be sending results of an analysis to someone who is only interested in that -- but I don't have to do a separate write-up from my code. I can have tables, multiple figures, notes -- everything in one place, in one document (can compile into a single, sharable document.
</aside>
---
## Multiple Audiences
![audiences](images/audiences.jpg)
<aside class="notes" data-markdown>
Lecture Notes
Assignments/Problem Sets
Internal vs. External
Create instructor versions with notes, assignment, and answer key versions of problem sets or lecture notes all from the same source document.
Set which chunks of code or text will be displayed or evaluated with variables.
</aside>
---
## Kindness to Future Self
![kind](https://media.giphy.com/media/26gmFUFEvc3cuZiak/giphy.gif)
<aside class="notes" data-markdown>
One important audience is your future self.
Typing comment lines in R can be cumbersome. That approach works well for scripts, but for analysis, you need more words.
I'm guessing I'm not the only one who has opened up an old analysis script and had no idea what I was doing or thinking. Why did I exclude those cases? How did I decide on that model specification?
With R Markdown, writing text with the code is easy, which makes it more likely that you'll do it!
</aside>
---
# How does it work? {data-background=#401F68}
---
![R Markdown Flow](images/RMarkdownFlow.png)
<div class="caption">Source: [R Markdown Website](http://rmarkdown.rstudio.com/lesson-2.html)</div>
<aside class="notes" data-markdown>
Markdown lets you write plain text, with some basic formatting.
**R** Markdown lets you add chunks of R code to a markdown document.
When you compile or "knit" the document, R Markdown then executes those code chunks and incorporates the output and results into a markdown document.
That markdown document with the text, code, and results is then compiled into html (for the web), pdf, or Word document, in a variety of different formats.
</aside>
---
## R Markdown
<pre>
---
output:
html_document:
keep_md: true
---
# Header 1
Markdown text, **bold**, *italics*,
[a link](http://google.com).
```{r}
summary(cars)
```
</pre>
---
## Markdown
<pre>
# Header 1
Markdown text, **bold**, *italics*,
[a link](http://google.com).
```r
summary(cars)
```
```
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
```
</pre>
---
## HTML Code
<pre>
<h1>Header 1</h1>
<p>Markdown text, <strong>bold</strong>,
<em>italics</em>,
<a href="http://google.com">a link</a>.</p>
<pre class="r"><code>summary(cars)</code></pre>
<pre><code>## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00</code></pre>
</div>
</pre>
---
# Header 1
Markdown text, **bold**, *italics*,
[a link](http://google.com).
```{r}
summary(cars)
```
---
![](images/simplehtml.jpeg)
---
## Compilation Options
RStudio Knit Button
![knit](images/knitbutton.jpeg)
`rmarkdown::render()`
---
![chunks](images/chunks.png)
<div class="caption">Source: [Heike Hofman](https://stat585-at-isu.github.io/materials/01_collaborative-environment/03_rmarkdown.html#)</div>
---
## YAML Header
Spacing matters
Look up the options: [R Markdown Formats](http://rmarkdown.rstudio.com/formats.html)
<aside class="notes" data-markdown>
Spacing - indentation, where colons and other punctuation are, etc. -- pay attention
You can override options set here with a call to render
</aside>
---
## Text Chunks: Markdown
```
# Header 1
## Header 2
### Header 3
*Italics*
**Bold**
[link text](http://google.com)
![caption][images/image.png]
- List Item 1
- List Item 2
- Item 2a
- Item 2b
1. Numbered list item 1
1. Numbered list item 2
```
<aside class="notes" data-markdown>
The second 1 in the numbered list isn't a typo -- makes it easier to reorder points
Markdown is also used on GitHub. It's easy for including formatted code chunks. It's plain text, but still gives you some formatting capabilities.
</aside>
---
![chunks](images/chunks.png)
<div class="caption">Source: [Heike Hofman](https://stat585-at-isu.github.io/materials/01_collaborative-environment/03_rmarkdown.html#)</div>
---
## Options chunk
<pre>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
</pre>
<aside class="notes" data-markdown>
This isn't necessarily required
and what's in this example doesn't change the default behavior
</aside>
---
## R chunks
<pre>
```{r chunkname}
a<-2
a + 1
a<-3
a + 2
```
</pre>
<aside class="notes" data-markdown>
chunk names will get auto generated if you don't set them
chunk names need to be unique -- can come up if automatically generating chunks (a bit of a hack)
if creating external figures, chunk names will get used
can make it easier to find errors
</aside>
---
```{r chunkname}
a<-2
a + 1
a<-3
a + 2
```
<aside class="notes" data-markdown>
Chunk was automatically divided
</aside>
---
## Plot
<pre>
```{r, fig.width=8, echo=FALSE}
plot(cars)
```
</pre>
---
```{r, fig.width=8, echo=FALSE}
plot(cars)
```
---
## Pretty Output with `knitr::kable`
---
```{r}
head(mtcars)
```
---
<pre>
```{r, results='asis'}
library(knitr)
kable(head(cars))
```
</pre>
```{r, echo=FALSE, results='asis'}
library(knitr)
kable(head(mtcars))
```
<aside class="notes" data-markdown>
kable creates table in format appropriate for output type
results asis prevents further processing of output
</aside>
---
```{r}
summary(lm(mpg ~ hp + disp, data=mtcars))
```
---
<pre>
```{r, results='asis'}
reg1 <- lm(mpg ~ hp + disp, data=mtcars)
kable(summary(reg1)$coef, digits=2)
```
</pre>
```{r, echo=FALSE, results='asis'}
reg1 <- lm(mpg ~ hp + disp, data=mtcars)
kable(summary(reg1)$coef, digits=2)
```
<aside class="notes" data-markdown>
other options/packages exist too: pander, xtable
</aside>
---
## Interactive Output<br>`DT::datatable`
<pre>
```{r, echo=FALSE}
library(DT)
datatable(mtcars, rownames = FALSE)
```
</pre>
---
```{r, echo=FALSE}
library(DT)
datatable(mtcars, rownames = FALSE)
```
---
![datatable](images/datatable.jpeg)
---
# {data-background=#40e0d0}
![happy](https://media.giphy.com/media/PBM14uaJCbp72/giphy.gif)
---
# When not to use R Markdown {data-background=#401F68}
<i class="fa fa-thumbs-o-down fa-3x"></i>
---
## Long data processing and import scripts
<aside class="notes" data-markdown>
You can include scripts from R Markdown documents, but just keep a separate scripts file.
</aside>
---
## Long-running or complex calculations
<aside class="notes" data-markdown>
For long-running code, consider having a separate script pre-produce the data or output if you want to use R markdown for presentation. You can knit/render the R markdown presentation file at the end of re-running your analysis to keep everything up to date.
</aside>
---
## Precise formatting required
<aside class="notes" data-markdown>
There are templates for a few journals and you can create your own, but markdown is intentionally limited in the formatting it allows.
I sometimes see questions of "How do I do X in markdown?" and a legitimate answer to that question is often: "You don't"
You can use some html and LaTeX directly for special cases (this presentation for example has a custom CSS style sheet, uses the Reveal JS javascript package, and has some html code directly in the file)
But if you need precision control there are better ways.
You can still use many of the packages used by R markdown to produce well formatted tables and other output to include in your reports.
Still scripting, but a different approach to putting together a document.
</aside>
# R Notebooks {data-background=#401F68}
[Link](http://rmarkdown.rstudio.com/r_notebooks.html)
<aside class="notes" data-markdown>
Moving R Markdown documents in the direction of Jupyter notebooks, with code chunks and the resulting output directly underneath.
Also adding some formatting and display "magic" and niceness automatically
Advantages are that you can see everything together
But I've found my workflow not to be as smooth, and the experience of using R Notebooks to not quite be 100% yet.
A different approach of having everything in the notebook file, versus composing a document where you might be selective about what's included.
</aside>
<aside class="notes" data-markdown>
</aside>