-
Notifications
You must be signed in to change notification settings - Fork 109
/
rmarkdown_tutorial.Rmd
217 lines (167 loc) · 5.72 KB
/
rmarkdown_tutorial.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
# (PART) Communicating Results {-}
# Rmarkdown tutorial
Author: Shaofeng Wu, Mingrui Liu
## 1. Overview
R Markdown provides a tidy framework for data science. A markdown file can normally help us: \
- **save and execute code that you wrote**\
- **generate high quality reports that can be shared with an audience**\
R Markdown documents are fully reproducible and support dozens of static and dynamic output formats.\
This link provide a quick tour of what's possible with R markdown.-[links](https://vimeo.com/178485416)
### 1.1 What is R Markdown?\
- **Rmd files**\
· An R Markdown(.Rmd) file is a record of your project. It contains the code that your audience needs to reproduce your work as well as all the scripts that your audience can understand what you did.\
- **Reproducible Research**\
· You can use `Knit` to rerun the code in an R Markdown file to reproduce your work and export the results to be a well-formatted report.\
- **Dynamic Documents**\
· There are a lot of ways to export your report. The formats include html, pdf, MS Word, or RTF documents; html or pdf based slides, Notebooks, and more.\
### 1.2 Workflow
(1) **Open a new .Rmd file** at File ▶ New File ▶ R Markdown. \
(2) **Write document** by editing template\
(3) **Knit document** to create report; use knit button or
**render()** to knit\
(4) **Preview Output** in IDE window\
(5) **Publish** (optional) to web server\
(6) **Examine build log** in R Markdown console\
(7) **Use output file** that is saved along side .Rmd
![Caption](resources/rmarkdown_tutorial/rmarkdownflow.png)
## 2. Getting started
### 2.1. Install the package
You can use the following command to install the required library.
```{r, eval=FALSE}
install.packages('rmarkdown')
```
### 2.2. Open file
You can create a new file or open existed from the directory that you choose.
### 2.3. output format
The Rmarkdown can render any Rmd file into a format that Rmarkdown supports. For example, the code below renders OutputExample.Rmd to a Microsoft Word document.
```{r results='hide'}
library(rmarkdown)
render("resources/rmarkdown_tutorial/OutputExample.Rmd", output_format = "word_document")
```
Following is a table of all the formats that you can choose:
**Output value table**
| output value | creates |
|--------------|---------|
| html_document| html|
| pdf_document |pdf (requires Tex )|
| word_document| Microsof Word (.docx)|
| odt_document |OpenDocument Text|
| rtf_document |Rich Text Format|
| md_document |Markdown|
| github_document| Github compatible markdown|
| ioslides_presentation| ioslides HTML slides|
| slidy_presentation| slidy HTML slides|
| beamer_presentation| Beamer pdf slides (requires Tex) |
You can choose to render to the format that you want by clicking the dropdown menu beside the knit button:
![Caption](resources/rmarkdown_tutorial/output.png)
## 3. Markdown syntax
Rmarkdown has lots of fancy syntax so that you can produce an ordered and beautiful document.\
We will give the syntax here that we regularly use a lot.
* **plain text**
plain text
* **italics and bold**
```{}
*italics* and **bold**
```
*italics* and **bold**
* **list**
```{}
* unordered list
+ sub-item 1
+ sub-item 2
- sub-sub-item 1
```
* unordered list
+ sub-item 1
+ sub-item 2
- sub-sub-item 1
```{}
1. ordered list
2. item 2
i) sub-item 1
A. sub-sub-item 1
```
1. ordered list
2. item 2
i) sub-item 1
A. sub-sub-item 1
* **headers**
```{}
# Header1 {#anchor}
## Header 2 {#css_id}
### Header 3 {.css_class}
#### Header 4
##### Header 5
###### Header 6
```
![Caption](resources/rmarkdown_tutorial/headers.png)
* **hyperlink**
```{}
<http://www.rstudio.com>
[link](www.rstudio.com)
Jump to [Header 1](#anchor)
```
<http://www.rstudio.com>
[link](www.rstudio.com)
Jump to [Header 1](#anchor)
* **table**
```{}
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
| 12 | 12 | 12 | 12 |
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |
```
| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
| 12 | 12 | 12 | 12 |
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |
* **equation**
```{}
$$E = mc^{2}$$
```
$$E = mc^{2}$$
## 4. Embeding code
### 4.1. Inline code
You can surround code with back ticks and r. R will replace inline code with its results.
For example:
```{r, eval=FALSE}
one plus one equals `r 1+1`
```
The output is:
one plus one equals `r 1+1`
### 4.2. Code chunks
In this way, ou need to start a chunk with \`\`\` {r} and end a chunk with \`\`\`
For example:
```{r}
print("Hello, world!")
```
### 4.3. Display options
There are also a lot of options to display your codes and results.
For example, you can choose `eval` as TRUE or False in the output to decide whether to evaluate the code and include its results.
Here is the difference between the two options:
With `eval = TRUE`:
```{r eval=TRUE}
print("Hi there!")
```
With `eval = FALSE`:
```{r eval=FALSE}
print("Hi there!")
```
Here is a table that includes the options that we normally use:
| option | default | effect |
|--------------|---------|---------|
| eval | TRUE | Whether to evaluate the code and include its results |
|echo|TRUE|Whether to display code along with its results|
|warning|TRUE| Whether to display warnings|
|error |FALSE |Whether to display errors|
|message| TRUE |Whether to display messages|
|tidy| FALSE |Whether to reformat code in a tidy way when displaying it|
|cache| FALSE| Whether to cache results for future renders|
|comment| "##" |Comment character to preface results with|
## 5. Rendering
* **first way**
You can run `rmarkdown::render("<file path>")` in the console.
* **second way**
You can click the `Knit` button in the top pane and choose the output format that you want.