-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
386 lines (278 loc) · 11.7 KB
/
index.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
---
title: |
![](www/img/interactive_logo.png){width=120px align=left style="padding-right: 20px"}
Making Interactive Case Studies Tutorial
css: www/style.css
output:
html_document:
includes:
in_header: www/GA_Script.Rhtml
self_contained: yes
code_download: yes
highlight: tango
number_sections: no
theme: cosmo
toc: yes
toc_float: yes
pdf_document:
toc: yes
word_document:
toc: yes
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(include = TRUE, comment = NA, echo = TRUE,
message = FALSE, warning = FALSE, cache = FALSE,
fig.align = "center", out.width = '90%')
library(knitr)
library(here)
# additional libraries needed
library(learnr)
library(gradethis)
gradethis::gradethis_setup()
```
<!-- Open all links in new tab-->
<base target="_blank"/>
<p> </p>
# **1. Setting Up**
***
## **`www` Directory**
***
Only the files in this `www` directory will be published with the `index.Rmd`.
In this `www` directory, we need to have:
* `style.css`
* `GA_Script.Rhtml`
* move the `data`, `docs`, `img`, etc. directories of the static version into the `www` directory. These directories contain the files needed to render the `index.Rmd` file.
* Additionally, we need to make an **`exercise` sub-directory**. This is where we would put the `.rda` files for the "setup" of the exercises.
## **YAML Header**
***
Refer to the YAML header of this `.Rmd` file.
Important notes (the followings are different from the static version):
* title
* `css: www/style.css` (the statics version does not have the `www` directory)
* `in_header: www/GA_Script.Rhtml` (the statics version does not have the `www` directory)
* the addition of `runtime: shiny_prerendered`
## **R Setup Chunk**
***
Refer to the setup chunk of this `.Rmd` file.
The main difference here is the addition of the the following libraries:
```{r, eval=FALSE}
library(learnr)
library(gradethis)
gradethis::gradethis_setup()
```
*Note: you might need to install `gradethis` from [github](https://github.com/rstudio/gradethis)*
## **Translate**
***
Use the following code for translation
```
<div align="left" id="google_translate_element",></div>
<script type="text/javascript" src='//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit'></script>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
```
*Note: this is different from the statics version. There is an extra "comma" at the end of `id="google_translate_element"`. The translation tool would not work without this comma (not sure why).*
## **Table of Content**
***
You might want to get rid of the following css code from the static version
```
<style>
#TOC {
background: url("https://opencasestudies.github.io/img/icon-bahi.png");
background-size: contain;
padding-top: 240px !important;
background-repeat: no-repeat;
}
</style>
```
Unlike in the static version, this would not add the logo to the table of content in the interactive app. Could work on how to add logo to the toc for interactive apps.
## **Optional: Add `exercise_block` to `www/style.css`**
***
You could add a specific style for the exercises by adding the following code to the `style.css` file:
```
.exercise_block {
padding: .5em;
border: 1px solid lightgrey;
background: white;
color: black;
}
```
#### {.exercise_block}
This is what the exercise block looks like. You might modify it if you want.
####
# **2. Modifications of the Static Version**
***
## **`data`, `docs`, `img`, etc. Directories are now Sub-Directories of `www`**
***
The file paths in the case study index.Rmd file that refer to these directories will need to be edited to reflect the new parent directory `www`. There are two cases of code chunks that will need to be edited slightly differently. I will use some examples to explain this. **A quick way to find where needs to be modified is to do `CTRL + F` and search for `here(`**
### Case 1: Static version chunk set to `echo = FALSE`
***
*When the static version has `echo = FALSE`:*
![](www/img/includegraphics.PNG)
***
*Simply add the `www` directory to the interactive version:*
![](www/img/includegraphics_new.PNG)
***
### Case 2: Static version chunk not set to `echo = FALSE`
***
*When the static version does not have `echo = FALSE`:*
![](www/img/load.PNG)
***
*Set the original chunk to `eval = FALSE` and the new chunk to `echo = FALSE` in the interactive version:*
![](www/img/load_new.PNG)
***
# **3. Add Exercises**
***
Each exercise chunk should have their own name. For example, `dw_code1` for the first coding problem embedded in the analysis of the "Data Wrangling" section, `dw_quiz` for the quiz of the "Data Wrangling" section, `dv_exercise1` for the first coding exercise of the "Exercise" subsection of the "Data Visualization" section, etc.
## **Coding Problems Embedded in the Analyses**
***
These could be the "Question Opportunities" ,or when an analysis is repeated with a different dataset and we want the students to come up with the code using what they learned, etc.
* First, before the exercise setup chunk, we need to save the dataset needed in the exercise as a `.rda` file and put it in the `exercise` sub-directory of the `www` folder.
* Then, in the exercise setup chunk, we need to load whatever libraries needed for this exercise that are not loaded in the `index.Rmd` setup chunk. In addition, we need to load the `.rda` made in the first step.
* Then, we can start making the exercise.
* Finally, when necessary, after the exercise, we can make a click to expend section containing the code and the output so that students may choose to skip these exercises.
Here is an example from the obesity case study:
![](www/img/exercise1.PNG)
![](www/img/exercise2.PNG)
## **The Exercise Subsection**
***
Each section (data wrangling, data visualization, etc.) could have their "Exercise" subsection as the last subsection.
### **Quizzes**
***
Following is an example from the CO2 case study:
```{r dw_quiz, echo = FALSE}
quiz(caption = "",
question("Which one of the following functions in the `dplyr` package allows us to see all of the variables (columns) at once, where several values of those columns are shown on the right of the variable names?",
answer("`slice_head()`", message = "This function allows us to see just the first rows of the data."),
answer("`slice_sample()`", message = "This function allows us to see a selection of random rows of the data."),
answer("`slice_tail()`", message = "This function allows us to see the rows at the end of the data."),
answer("`glimpse()`", correct = TRUE),
allow_retry = TRUE,
random_answer_order = TRUE
),
question("Which one of the pipe operators (from the `magrittr` package) should be used right after a variable name if we want to perform a sequence of operations on that variable, and meanwhile, assign the final output to that variable (without redefining that variable using `<-` or `=`)?",
answer("`%>%`", message = "This operator cannot assign the final output to that variable."),
answer("`%<%`", message = "This is not a valie pipe operator."),
answer("`%T>%`", message = "This is the side-effects operator."),
answer("`%<>%`", correct = TRUE),
allow_retry = TRUE,
random_answer_order = TRUE
),
question("Which of the following can the `mutate()` function in the `dplyr` package do? (more than one correct answers)",
answer("Select certain variables(columns) of the data.", message = "Selecting certain variables(columns) of the data is the function of the `select()` function."),
answer("Rename a variable.", message = "Renaming a variable is the function of the `rename()` function."),
answer("Create a new variable.", correct = TRUE),
answer("Modify an existing variable.", correct = TRUE),
allow_retry = TRUE,
random_answer_order = TRUE
)
)
```
### **Coding Exercise**
***
* Following is an example from the CO2 case study **without use of the `gradethis` package**:
#### {.exercise_block}
We have created a dataset called `wide_format` that is in "wide" format. Write some code to convert it to "long" format. (We want 3 columns with names "ID", "Variable", and "Value".)
```{r dw_exercise1-setup}
ID <- c(1, 2, 3)
Year <- c(2015, 2018, 2019)
Length <- c(20, 21.5, 19)
wide_format <- data.frame(ID, Year, Length)
```
```{r dw_exercise1, exercise=TRUE, exercise.eval=TRUE, exercise.lines = 5}
wide_format
```
```{r dw_exercise1-hint}
wide_format %>%
pivot_longer()
```
```{r dw_exercise1-solution}
wide_format %>%
pivot_longer(cols = -ID, names_to = "Variable", values_to = "Value")
```
####
<p> </p>
* Following is an example from the CO2 case study **with use of the `gradethis` package**:
#### {.exercise_block}
`mtcars` is a built-in R dataset as shown below. Use this dataset to complete the following exercise.
```{r}
mtcars
```
Write some code that will:
1. Create a new dataset called `new.mtcars` from `mtcars`.
2. Create a new column called `car` in `new.mtcars` that uses the values from the row names of `mtcars`. (Use the `rownames_to_column` function from the `tibble` package. See documentation [here](https://tibble.tidyverse.org/reference/rownames.html){target="_blank"}.)
3. Only keep the `car`, `mpg`, and `cyl` columns of this `new.mtcars` dataset.
4. Only keep the cars that have `cyl` = 4.
5. Rearrange the dataset by `mpg` in descending order.
*Note: replace the underscores("\_") with your code with one character per "\_". Click "Submit Answer" to check your answer. *
```{r dw_exercise2-setup}
library(tidyverse)
```
```{r dw_exercise2, exercise=TRUE, eval=FALSE}
# step 1
new.mtcars <- mtcars %>%
# step 2
rownames_to_column(var = "___") %>%
# step 3
______(___, ___, ___) %>%
# step 4
______(___ == _) %>%
# step 5
_______(____(mpg))
# view data (Do NOT include this part when submitting your answer!)
# new.mtcars
```
```{r dw_exercise2-hint-1}
# step 2:
# The "var" argument is the name of the new column that will use the values from the row names. What is the column name?
```
```{r dw_exercise2-hint-2}
# step 2:
# The "var" argument is the name of the new column that will use the values from the row names. What is the column name?
# step 3:
# Which function of the "dplyr" package select columns?
```
```{r dw_exercise2-hint-3}
# step 2:
# The "var" argument is the name of the new column that will use the values from the row names. What is the column name?
# step 3:
# Which function of the "dplyr" package select columns?
# step 4:
# Which function of the "dplyr" package subset rows?
```
```{r dw_exercise2-hint-4}
# step 2:
# The "var" argument is the name of the new column that will use the values from the row names. What is the column name?
# step 3:
# Which function of the "dplyr" package select columns?
# step 4:
# Which function of the "dplyr" package subset rows?
# step 5:
# Which function of the "dplyr" package will reorder the data?
# What argument do we need to reorder in descending order?
```
```{r dw_exercise2-hint-5}
# Next hint will show you the solution
```
```{r dw_exercise2-solution}
new.mtcars <- mtcars %>%
rownames_to_column(var = "car") %>%
select(car, mpg, cyl) %>%
filter(cyl == 4) %>%
arrange(desc(mpg))
```
```{r dw_exercise2-check, eval=FALSE}
grade_code()
```
####
# **4. Maintenance Notes**
***
See the `maintenance_notes.Rmd` file as an example.
Make sure to list the names of the exercise chunks for future maintenance. Except for things mentioned in this tutorial, other changes should be noted as well.
![](www/img/maintenance.PNG)
# **5. Publishing**
***
Make sure the `rsconnect` folder is also pushed to GitHub.