-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcookbook-doc_yours.Rmd
235 lines (147 loc) · 5.93 KB
/
cookbook-doc_yours.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
# Make it yours {#doc-yours}
```{r, include = FALSE}
source("common.R")
knitr::opts_chunk$set(fig.align="center")
```
```{r, echo= FALSE}
knitr::include_graphics("images/illos/yours-doc.jpg")
```
Now it is time to personalize your R Markdown document to make it a lesson that works for you and your learners.
## Add code {#doc-code}
<div class = "side-by-side">
<div class = "side1">
\
````
```{r}`r ''`
# a code comment
sample(c("fun", "fancy"), 1)
```
````
</div>
<div class = "side2">
You can insert new code chunks into your document in the RStudio IDE by putting your cursor in your doc where you want to plant your new chunk, then clicking on the ![](images/doc-insert-chunk.png){width=5%} button.
This adds the chunk delimiters `` ```{r} `` and `` ``` `` for you, and you place your code inside. Use `#` to add comments to your code.
</div>
</div>
## Embrace Markdown {#doc-md}
<div class = "side-by-side">
<div class = "side1">
```{r echo = FALSE, fig.link=c("https://commonmark.org/help/tutorial/",NA)}
knitr::include_graphics("images/screenshots/md-tutorial.png")
knitr::include_graphics("images/screenshots/rstudio-md-quick.png")
```
</div>
<div class = "side2">
\
One of the not-very-secret weapons of the most skilled R Markdown users is knowing Markdown inside and out. If you haven't done so already, take 10 minutes to do an [interactive Markdown tutorial](https://commonmark.org/help/tutorial/).
\
\
\
\
From within the RStudio IDE, you can also access a *"Markdown Quick Reference"* from the **Help** menu. This renders a cheatsheet-style document in your **Help** viewer pane, so you can try to jog your memory without leaving your project.
</div>
</div>
## Add structure with headers {#doc-headers}
Headers are useful for organizing your document into sections, and with Markdown formatting you can have up to 6 types/sizes of headers. Use a `#` symbol before the label to create a new header.
<div class = "split">
<div class = "split1">
```markdown
# HEADER 1
## HEADER 2
### HEADER 3
#### HEADER 4
##### HEADER 5
###### HEADER 6
```
</div>
<div class = "no-anchor">
<h1 style="margin-top: 0em;">Header 1</h1>
<h2 style="margin-top: 0em;">Header 2</h2>
<h3 style="margin-top: 0em;">Header 3</h3>
<h4 style="margin-top: 0em;">Header 4</h4>
<h5 style="margin-top: 0em;">Header 5</h5>
<h6 style="margin-top: 0em;">Header 6</h6>
</div>
</div>
\
The RStudio IDE has several built-in features made to improve quality of life for R Markdown users.
\
<div class = "side-by-side">
<div class = "side1">
```{r md-nav, echo=FALSE, fig.cap="RStudio code navigator", fig.link='https://r4ds.had.co.nz/', fig.align="center", out.width = "80%"}
knitr::include_graphics("https://r4ds.had.co.nz/screenshots/rmarkdown-chunk-nav.png")
```
</div>
<div class = "side2">
One feature is the drop-down code navigator in the bottom-left of your file editor as shown in Figure \@ref(fig:md-nav) (from the [R for Data Science book](https://r4ds.had.co.nz/)).
As you can see, this uses your Markdown headers to allow you to navigate around your `.Rmd` file.
</div>
</div>
\
You can also use the RStudio outline pane, as shown in Figure \@ref(fig:md-outline) (from the [RStudio blog](https://rviews.rstudio.com/2016/11/11/easy-tricks-you-mightve-missed/)).
```{r md-outline, echo=FALSE, fig.cap="RStudio outline pane", out.width="80%", fig.link='https://rviews.rstudio.com/2016/11/11/easy-tricks-you-mightve-missed/', fig.align="center"}
knitr::include_graphics("https://www.rstudio.com/wp-content/uploads/2016/11/tip_outline.gif")
```
## Add narrative {#doc-narrative}
Start writing! You can add words anywhere in your document below the YAML, just be sure you aren't adding non-R-code inside any code chunks.
Let's take a lightly-edited snippet from a [Wikipedia about "The Great British Bake Off"](https://en.wikipedia.org/wiki/The_Great_British_Bake_Off). On the left, you can see the raw Markdown and on the right, the rendered version.
<div class = "split">
<div class = "split1">
```markdown
# The Great British Bake Off
The Great British Bake Off (also called
*Bake Off* or *GBBO*) is a British
television baking competition.
In the first series, the location of
the cast and crew moves from town to town
each week, with a different baking
**theme** each week:
1. Cakes
1. Biscuits
1. Bread
1. Puddings
1. Pastry
1. Tea Party
In each episode, the amateur bakers
are given three challenges based on
that week's theme:
- A signature bake,
- a technical challenge,
- and a show-stopper.
[Photo:](https://en.wikipedia.org/wiki/Macaron)
Vanilla Macaron, Michelle Naherny
![](http://bit.ly/vanilla-macaron)
```
</div>
<div class = "no-anchor">
<h4 style="margin-top: 0em;">The Great British Bake Off</h4>
The Great British Bake Off (also called *Bake Off* or *GBBO*) is a British television baking competition.
In the first series, the location of the cast and crew moves from town to town each week, with a different baking **theme** each week:
1. Cakes
1. Biscuits
1. Bread
1. Puddings
1. Pastry
1. Tea Party (Final)
In each episode, the amateur bakers are given three challenges based on that week's theme:
- A signature bake,
- a technical challenge,
- and a show-stopper.
[Photo:](https://en.wikipedia.org/wiki/Macaron)
Vanilla Macaron, Michelle Naherny
![](http://bit.ly/vanilla-macaron)
</div>
</div>
## Add space {#doc-breathe}
Let your words and code chunks breathe! This is unfortunately a bit manual. But--if you don't plan on doing any CSS, then we highly recommend doing the below:
* **Add white space** in the body of your .Rmd file by including either a `<br>` or a `\` on its own line with a blank line above and below it. At a minimum, we recommend doing before top-level section headers.
:::hat
> "Code needs a lot of whitespace. That's how it breathes."
>
> — [Roger Peng](https://twitter.com/Dale_Masch/status/1017651666999394304?s=20)
:::
<div class = col2>
![](images/rmd_custom/squished1.png)
<br>
![](images/rmd_custom/white_space1.png)
</div>