/
Complete_Example.Rmd
375 lines (249 loc) · 10.8 KB
/
Complete_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
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
---
title: "Knitr and R Markdown for Reproducible Workflow"
author: "Laura Wiley"
date: "`r format(Sys.time(),'%B %d, %Y')`"
output:
html_document:
toc: true
---
```{r global_options, include=FALSE}
## This will set global options for all chunks. They can be overwritten at each chunk as desired. This saves time if you never want to display code (e.g., echo=FALSE as default)
#knitr::opts_chunk$set(fig.width=12, fig.height=8, fig.path='Figs/',echo=FALSE, warning=FALSE, message=FALSE)
```
# Introduction to Markdown
Formatting documents in markdown requires knowledge of a few syntactic symbols.
## New Paragraphs
To get spacing between paragraphs, you need to have two spaces at the end of a line.
_Single or no space at end of the line:_
Paragraph 1
Paragraph 2
_Double space at the end of the line:_
Paragraph 1
Paragraph 2
## Text Formatting
### Italics
You can get italics using either a single asterisk or underscore
```{italics}
*asterisk* or _underscore_
```
*asterisk* or _underscore_
### Bold
You can get bold text using either a double asterisk or double underscore
```{bold}
**double asterisk** or __double underscore__
```
**double asterisk** or __double underscore__
### Superscript and Subscript
Use carets to get superscripts and tildes for subscripts.
```{super_subscript}
x^superscript^ and x~subscript~
```
x^superscript^ and x~subscript~
### Strikethrough
Use a double tilde for strikethrough formatting
```{strikethrough}
~~scratch that~~
```
~~scratch that~~
## Other Text Types
### Weblinks
Include the text you want displayed in brackets, with the weblink in parentheses.
```{weblinks}
[This is a link to tonight's presentation.](https://github.com/laurakwiley/KnitrandRMarkdown_NashvilleRUsers)
```
[This is a link to tonight's presentation.](https://github.com/laurakwiley/KnitrandRMarkdown_NashvilleRUsers)
### Equations
You can use standard LaTeX equations in markdown with the dollarsign syntax.
```{equations}
$A = \pi*r^{2}$
```
$A = \pi*r^{2}$
### Lists
#### Unordered
Just use an asterisk at the front of the line followed by a space and then your text. To get the next level down the heirarchy use two tabs or four spaces in RStudio. You can continue to use an asterisk or you can use a plus sign,
```{bullets}
* Highest Level
* Same symbol sub-level
+ Changed symbol sub-level
* Sub-sub-level
* Next Topic
```
* Highest Level
* Same symbol sub-level
+ Changed symbol sub-level
* Sub-sub-level
* Next Topic
#### Numbered
Use numbering like you normally would. Again put two tabs or four spaces to move down the heirarchy.
```{numbered_list}
1. First item
a. ordered sub-item
2. Second item
* bulleted sub-item
+ bulleted sub-item
```
1. First item
a. ordered sub-item
2. Second item
* bulleted sub-item
+ bulleted sub-item
### Manually-Entered Tables
Use verticle bars and dashes for your table separators. Can make it pretty, but you don't have to for it to format properly.
```{tables}
Column 1 | Column 2
-----------------------|-----------
cell 1 | cell 2
Lots of data in cell 3 | cell 4
Column 1 | Column 2
-|-
cell 1 | cell 2
Lots of data in cell 3 | cell 4
```
Column 1 | Column 2
-----------------------|-----------
cell 1 | cell 2
Lots of data in cell 3 | cell 4
Column 1 | Column 2
-|-
cell 1 | cell 2
Lots of data in cell 3 | cell 4
# Integrating RMarkdown with R Code
## Inline Code
Let's say you have templated text that you want to import data into. For instance, if I was a botanist, I might want to have the following template for my employees/students to report out the data they collected that week.
This week, I measured data on XX species of plants. The average sepal length and width were YY and ZZ respectively with standard deviations of AA and BB. The average petal lengths and widths (standard deviation) were CC (DD) and EE (FF) respectively.
Using the iris data set, I can automatically run R code inline using backticks ` `.
This week, I measured data on `r length(unique(iris$Species))` species of plants. The average sepal length and width were `r mean(iris$Sepal.Length)` and `r mean(iris$Sepal.Width)` respectively with standard deviations of `r sd(iris$Sepal.Length)` and `r sd(iris$Sepal.Width)`. The average petal lengths and widths (standard deviation) were `r mean(iris$Petal.Length)` (`r sd(iris$Petal.Length)`) and `r mean(iris$Petal.Width)` (`r sd(iris$Petal.Width)`) respectively.
## Code Chunks
This is the most common way of integrating R code in RMarkdown documents. You use three backticks to set off each chunk, then curly braces to explain the language used (for syntax highlighting), a chunk name, and any options.
```{r example_code_chunk_default_options}
head(iris)
```
By default, these code chunks display the R code run, as well as any console output. There are a number of options you can use to change these default parameters.
### Options
#### echo
When you are sending analyses to coworkers/analysts, you probably want to include the code used to generate your findings. However if you're sending it the C-Suite, you probably want to just show the output. echo=False, suppresses the R code from display.
```{r example_echo, echo=FALSE}
head(iris)
```
#### warning
Invariably when I load packages I get a warning from library("dplyr") about masking other functions. I really don't need to see these in my report, so I can use warning=FALSE.
```{r example_w_warning}
library("plyr")
library("dplyr")
```
```{r example_wo_warning, warning=FALSE}
library("plyr")
library("dplyr")
```
#### error
The error option tells you wether R should quit if it finds an error or keep going.
error=FALSE - R script stops and you cannot knit until the error is fixed.
error=TRUE - keeps running the R script but outputs an error message.
```{r example_error, error=TRUE}
my_nonfunction(testing)
```
#### results
You can also change the format of the results that you produce. There are four options: markup, asis, hold, or hide.
##### results="markup"
```{r results_markup, results='markup'}
head(iris)
```
##### results='asis'
Results='asis' returns just the raw text output to the console, without any formatting. In the case of head(iris) this is undesirable as the output is really messy.
```{r results_asis, results='asis'}
head(iris)
```
However, if you have a function that outputs markdown, then you want to leave the results "asis" so that they can be processed with the rest of your markdown document. A great example of a cleaner way to show data from a dataframe is to use the Knitr function kable(). For this function to work, you have to return results "asis".
```{r results_asis_kable, results='asis'}
library("knitr")
kable(head(iris))
```
##### results="hold"
If you have multiple returns of data inside a single code chunk, Knitr will display each piece of code and its results as there are data to return. If you would prefer instead to have all output from a chunk in a single output section, use results="hold".
Without Hold:
```{r results_no_hold}
cat('Inside a chunk\n\n')
for (i in 1:3) {
cat('* Inside loop #', i, '\n')
}
cat('Outside a loop, but still inside the chunk')
```
With Hold:
```{r results_hold, results='hold'}
cat('Inside a chunk\n\n')
for (i in 1:3) {
cat('* Inside loop #', i, '\n')
}
cat('Outside a loop, but still inside the chunk')
```
##### results="hide"
For some chunks you do not want to show any results. For this use results="hide".
```{r results_hide, results='hide'}
head(iris)
```
#### comment
If you've noticed, when we have raw output from R, it is always preceeded by two pound signs. This can be controlled with the comment option. I prefer just to have this turned off - e.g., no comment characters.
```{r comment_example, comment=""}
head(iris)
```
#### eval
Sometimes you want to document the code you used for a particular step, but you don't want it to execute while rendering the report. Use eval=FALSE to have R skip this code chunk.
```{r example_eval, eval=FALSE}
replicate(100000, rnorm(100000))
```
#### cache
An alternative to eval=FALSE, is to only run a chunk of code once and cache the results. This stores all data objects and figures created in that code chunk in a directory and simply pulls in that data for future code chunks. A word of caution, this works well if the computational analysis time is the slow step. If you are dealing with large datasets this may not be any faster because of the long read times to import the data.
```{r cache_example, cache=TRUE}
library("ggplot2")
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point()
```
## Figures
You can include figures in your RMarkdown document. There are a number of options to precisely control how these figures appear.
### Options
#### fig.path
This allows you to select a particular file name and location for a figure created in the graph. A word of caution, it concatenates with the chunk name!
```{r fig_path, fig.path="Complete_Example_files/figure-html/path_example_"}
library("ggplot2")
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,shape=Species))+geom_point()
```
#### fig.show
Like results, this option configures how the plots are displayed. This allows you to keep the content output of a chunk, but suppress or modify the figure.
##### 'asis'
```{r fig_asis, fig.show='asis'}
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point()
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,shape=Species))+geom_point()
```
##### 'hold'
```{r fig_hold, fig.show='hold'}
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point()
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,shape=Species))+geom_point()
```
##### 'hide'
```{r fig_hide, fig.show='hide'}
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point()
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,shape=Species))+geom_point()
```
#### fig.align
This allows you to move the figures around as desired.
```{r fig_left, fig.align='left'}
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point()
```
```{r fig_center, fig.align='center'}
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point()
```
```{r fig_right, fig.align='right'}
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+geom_point()
```
#### fig.width and fig.height
These allow you to change the size/dimensions of your figure. The default is 7" for both measurements.
```{r fig_default_size}
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+facet_grid(.~Species)
```
```{r fig_size,fig.height=7,fig.width=21}
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width))+geom_point()+facet_grid(.~Species)
```
# One last trick
Want to see all of the R code from a markdown document? Use the purl() command from knitr.
```{r purl, eval=FALSE}
purl("Complete_Example.Rmd")
```