-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab4.Rmd
363 lines (276 loc) · 12.1 KB
/
lab4.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
---
title: "Lab 4 - Introduction to RStudio and R Markdown. Presentations"
author: "Jonah Hamiilton"
date: "October 1st 2022"
output:
html_document:
toc: true
editor_options:
markdown:
wrap: 72
---
<!-- This is a comment in R Markdown (and regular markdown, such as in Jupyter)-->
<!-- The dashes below are not part of the YAML block above, -->
<!-- they insert a horizontal line after the ToC -->
------------------------------------------------------------------------
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
In Lab 4, you will learn how to use, edit and create a R Markdown
document (like this one) using RStudio. You should follow the
instructions in this document to complete the assignment. Knit this
document to view the nicely rendered HTML, which can make it easier to
read the questions.
If you need help as you use R Markdown in this lab and others in the
future, consult the following resources:
- [Cheat sheet](https://rmarkdown.rstudio.com/lesson-15.html)
- [Home page with guides](https://rmarkdown.rstudio.com/docs/)
- [Reference book](https://bookdown.org/yihui/rmarkdown/)
The below is a code chunk, but instead of using the `r` engine we're
creating and alert block that will make the question show up with a blue
background in the HTML output. Unfortunately, this creates and error
when exporting to PDF, so it can only be used for HTML.
```{block, type="alert alert-info"}
## Submission Instructions
rubric={mechanics:2}
You receive mark for submitting your lab correctly, please follow these instructions:
- [Follow the general lab instructions](https://ubc-mds.github.io/resources_pages/general_lab_instructions/).
- [Click here to view a description of the rubrics used to grade the questions](https://github.com/UBC-MDS/public/tree/master/rubric).
- Push your `.Rmd` AND **all the files** you will create as part of the lab to your GitHub repository.
- The reason for pushing all the files is that `.Rmd` does not contain the rendered output
from running the cells.
If someone is checking out your work
there needs to be an HTML file to view the output,
so it is good to get into this habit.
- `.ipynb` renders nicely on GitHub,
which is why we did not include the HTML file for previous labs.
- Upload a `.Rmd` version of your assignment to Gradescope.
- Include a clickable link to your GitHub repo for the lab just below this cell (it should look something like this https://github.ubc.ca/MDS-2022-23/DSCI_521_labX_yourcwl.
```
<https://github.ubc.ca/MDS-2022-23/DSCI_521_lab4_joham>
## Editing R Markdown documents
This document is called an R Markdown document. It is a literate code
document, similar to Jupyter notebooks where you can write code and view
its outputs. To start, let's set our working directory by creating a new
R Project for lab 4.
### Text and rendering R Markdown documents
In a R Markdown document any line of text not in a code chunk (like this
line of text) will be formatted using Markdown. Similar to JupyterLab,
you can also use HTML and LaTeX here to do more advanced formatting. To
run a code chunk, you can press the green play button in the top right
corner of the chunk.
```{block, type="alert alert-info"}
#### Question 1
rubric={correctness:1}
To render the HTML files we are going to create, the first step is to activate GitHub pages.
```
link to site: <https://joham63.github.io/523_lab_4_pages/>
```{block, type="alert alert-info"}
#### Question 2
rubric={mechanics:1}
Create a new code chunk below using the r language engine that runs some R code
(it does not need to be complicated,
but it should have an output).
Ensure that you can render/knit the document after you add that chunk.
```
Here is some R code
```{r Question 2 test chunk}
introduction <- "hello this is an introduction"
# prints out a welcome message
for (word in introduction) {
print(word)
}
```
```{block, type="alert alert-info"}
#### Question 3
rubric={mechanics:1}
Create a new code chunk,
and add a meaningful name to the code chunk.
Try using the pop-up-like menu to navigate between the named code chunks
Don't forget to knit/render the document
after you make this change to ensure everything is still working.
```
### Q 3 Quick Navigate Heading
```{r adding the first quick navigate heading}
# this chunk can now be accessed easily from the dropdown menu!
```
```{block, type="alert alert-info"}
#### Question 4
rubric={mechanics:1,reasoning:1}
Create a new code chunk that uses a code chunk option.
Write out in your own words what the code chunk option is doing.
```
### Q3 Code Chunk Option
```{r code chunk option test, eval=FALSE}
mystery_variable <- "I will never be assigned printed"
print(mystery_variable)
# The code chunk option above when set to FALSE will ensure the code is not evaluted and the results are not printed to the screen
```
#### Multiple code chunk options
To have multiple code chunk options you separate them by a comma. For
example, if in addition to suppressing warnings, we want to run the code
but not output the results, then we can add the `include = FALSE`
argument to the code chunk after the `warning = FALSE` option.
```{block, type="alert alert-info"}
#### Question 5
rubric={mechanics:1,reasoning:1}
Create a new code chunk that uses at least two code chunk options.
At least one must be different to the ones mentioned above.
Write in your own words what each code chunk option is doing.
```
### Question 3 multiple code chunk options
```{r multiple code option test, message=FALSE, prompt=TRUE, highlight=TRUE}
# The code chunk option above will prevent the code chunk from printing any messages and also the source code will be preceeded with the prompt ">" and be highlighted.
#pretty output
print(1+1)
print(1353/124)
print(23^3)
```
### 1.5. YAML Header and document output options
R Markdown files contains three types of content:
1. Plain text mixed with simple Markdown formatting.
2. Code chunks surrounded by <code>\`\`\`</code>.
3. An (optional) YAML header surrounded by `---`.
You have been introduced the first two types of content, but not the
third (although you probably saw it at the top of this document). The
(optional) YAML header, which is located at the very top of R Markdown
files sets some general global parameters, including:
- title
- author
- output
- etc
**Example YAML Header**
---
title: "Reproducible Data Science Report"
author: "Florencia D'Andrea"
date: "September 4, 2022"
output: html_document
---
Most important from a workflow perspective is **output**. Possible
output options include:
- `output: html_document`
- `output: md_document`
- `output: pdf_document`
- `output: word_document`
- `output: xaringan::moon_reader` (xaringan presentation - html)
```{block, type="alert alert-info"}
#### Question 6
rubric={mechanics:1}
Navigate to the YAML header at the very top of this document
and edit it so that you include an `author` (yourself) and a `date` (lab due date).
Include what you added below here as well as a fenced Markdown code block.
```
```{yaml}
---
title: "Lab 4 - Introduction to RStudio and R Markdown. Presentations"
author: "Jonah Hamiilton"
date: "October 1st 2022"
output:
html_document:
toc: true
editor_options:
markdown:
wrap: 72
---
```
### Creating R Markdown documents
You can use the "File" menu inside RStudio to create new R Markdown
documents by selecting: **File \> New File \> R Markdown** This will
bring you to another menu where you can choose the type of output (don't
be afraid to pick something, you can always change the `output` type
once you have the `.Rmd` file).
To create a written report, we generally recommend using the default
`output: html_document` as it is easier to read than PDF (note - LaTeX
does not render nicely in such documents sadly, so if you are using a
lot of LaTeX then you may want to choose `output: pdf_document`). If you
want to create an `.md` file to publish on GitHub, it is recommend to
instead use `output: github_document`. To get this from the menu above
you need to navigate to the "From Template" option on the left panel and
then select "GitHub Document (Markdown)".
```{block, type="alert alert-info"}
#### Question 7
rubric={mechanics:2}
1 - Create a new RMarkdown report (a different file than this one)
in the same directory as this RMarkdown file.
Use `html_document` as the `output`.
After you have rendered it, paste the link to the HTML output
as a link to your GitHub repository
(remember to push all your files!)
2 - Then, navigate to the YAML header at the very top of that `.Rmd` document
and edit it so that the `output` is `pdf_document`.
Then knit/render the document.
Note the different output.
Add and commit that rendered both the `.html` and `.pdf` files
to the GitHub repository for this lab
and paste the two links below this question.
```
html:<https://github.ubc.ca/MDS-2022-23/DSCI_521_lab4_joham/blob/master/521_lab4_RMarkdown_output_test.html>
pdf:
<https://github.ubc.ca/MDS-2022-23/DSCI_521_lab4_joham/blob/master/521_lab4_RMarkdown_output_test.pdf>
```{block, type="alert alert-info"}
#### Question 8
rubric={mechanics:6}
1. Go back to the `.Rmd` file you created in question 7,
and include at least two Markdown text sections
(each should have a header)
and at least two separate code chunks in it (these can be really simple).
Save the new R Markdown document and give it a new meaningful name.
2. Render/knit the new R Markdown document to get an `.html` file.
Put the `.Rmd` document
and the rendered `.html` file under version control using Git,
and push/upload the file to your GitHub repository for this homework.
Paste a link to these files as your answer below.
```
Rmd doc:
<https://github.ubc.ca/MDS-2022-23/DSCI_521_lab4_joham/blob/master/521_lab4_RMarkdown_output_test_version-2.Rmd>
html:
<https://github.ubc.ca/MDS-2022-23/DSCI_521_lab4_joham/blob/master/521_lab4_RMarkdown_output_test_version-2.html>
```{block, type="alert alert-info"}
#### Question 9 (Optional)
rubric={mechanics:1,reasoning:1}
1. Take the R Markdown report created in Question 8
and change the output to `github_document` and render it.
Put the rendered `.md` file under version control using Git,
and push/upload the file to your GitHub repository for this homework.
Try to look at the file on GitHub.ubc.ca in your homework repo?
What do you see? How is it rendered?
```
The .md file was rendered in the same style as github documents, the
same content is included compared to the pdf/html files but the style
and way it is presented is different,
```{block, type="alert alert-info"}
#### Question 10
rubric={mechanics:6}
1. Create a presentation using RStudio.
Do this in a different file than this one but in the same directory as this RMarkdown file.
You can use xaringan or Quarto to create the slides.
[On the book](https://pages.github.ubc.ca/MDS-2022-23/DSCI_521_platforms-dsci_students/materials/lectures/8-rmarkdown-quarto-slides-ghpages.html) you will find links
that will guide you on how to create each type of slide.
If you are not sure which one to use,
it is safer to use Xaringan as Quarto is quite new and
you will have to learn how to use `.qmd` files.
But if you want to explore Quarto,
we will accept both options (Xaringan or Quarto slides)
as correct.
Give this file a meaningful name.
2. Create at least 4 slides.
At least two slides must include a code chunk or cell (these can be really simple).
Save the new document.
3. Render/knit the new document to get a `html` presentation file.
4. Put the new document and the rendered `.html` file under version control using Git,
and push/upload the file to your GitHub repository for this lab
5. Activate GitHub pages and paste the link below.
```
YOUR ANSWER GOES HERE
```{block, type="alert alert-info"}
#### (Challenging) Question 11
rubric={reasoning}
In a paragraph or two,
compare and contrast the use of reproducible tools
(e.g., R Markdown and Jupyter)
and non-reproducible tools
(Word, Powerpoint, Keynote, etc)
for presentations and reports.
Include advantages and disadvantages for each.
```
YOUR ANSWER GOES HERE