-
Notifications
You must be signed in to change notification settings - Fork 13
/
exercises.Rmd
100 lines (60 loc) · 3.95 KB
/
exercises.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
---
title: "Web Exercises"
output: webex::webex_default
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
if (!requireNamespace("webex")) {
stop("You must have the 'webex' package installed to knit HTML from this template.\n devtools::install_github(\"psyteachr/webex\")")
} else {
library("webex")
}
```
This is a Web Exercise template created by the [psychology teaching team at the University of Glasgow](http://www.psy.gla.ac.uk), based on ideas from [Software Carpentry](https://software-carpentry.org/lessons/). This template shows how instructors can easily create interactive web documents that students can use in self-guided learning.
The webex package provides a number of functions that you use in [inline R code](https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf) to create HTML widgets (text boxes, pull down menus, buttons that reveal hidden content). Examples are given below. Knit this file to HTML to see how it works.
**NOTE: To use the widgets in the compiled HTML file, you need to have a JavaScript-enabled browser.**
## Total Correct (`total_correct()`)
The function `total_correct()` displays a running total of correct responses. Change the `elem` argument to display in a different style (e.g., `h2` or `h3` for header styles). If you're comfortable with css styles or classes, you can add them with the `args` argument.
`r total_correct(elem = "h3", args = "style='color:#003366;'")`
## Fill-In-The-Blanks (`fitb()`)
Create fill-in-the-blank questions using `fitb()`, providing the answer as the first argument.
- 2 + 2 is `r fitb("4")`
You can also create these questions dynamically, using variables from your R session.
```{r echo = FALSE}
x <- sample(2:8, 1)
```
- The square root of `r x^2` is: `r fitb(x)`
The blanks are case-sensitive; if you don't care about case, use the argument `ignore_case = TRUE`.
- What is the letter after D? `r fitb("E", ignore_case = TRUE)`
If you want to ignore differences in whitespace use, use the argument `ignore_ws = TRUE` (which is the default) and include spaces in your answer anywhere they could be acceptable.
- How do you load the tidyverse package? `r fitb(c("library( tidyverse )", "library( \"tidyverse\" )", "library( 'tidyverse' )"), ignore_ws = TRUE, width = "20")`
You can set more than one possible correct answer by setting the answers as a vector.
- Type a vowel: `r fitb(c("A", "E", "I", "O" , "U"), ignore_case = TRUE)`
You can use regular expressions to test answers against more complex rules.
- Type any 3 letters: `r fitb("^[a-zA-Z]{3}$", width = 3, regex = TRUE)`
## Multiple Choice (`mcq()`)
- "Never gonna give you up, never gonna: `r mcq(c("let you go", "turn you down", "run away", answer = "let you down"))`"
- "I `r mcq(c(answer = "bless the rains", "guess it rains", "sense the rain"))` down in Africa" -Toto
## True or False (`torf()`)
- True or False? You can permute values in a vector using `sample()`. `r torf(TRUE)`
## Hidden solutions and hints
You can fence off a solution area that will be hidden behind a button using `hide()` before the solution and `unhide()` after, each as inline R code. Pass the text you want to appear on the button to the `hide()` function.
If the solution is an RMarkdown code chunk, instead of using `hide()` and `unhide()`, simply set the `webex.hide` chunk option to TRUE, or set it to the string you wish to display on the button.
### Example problem
**Recreate the scatterplot below, using the built-in `cars` dataset.**
```{r echo = FALSE}
with(cars, plot(speed, dist))
```
`r hide("I need a hint")`
See the documentation for `plot()` (`?plot`)
`r unhide()`
<!-- note: you could also just set webex.hide to TRUE -->
```{r eval = FALSE, webex.hide="Click here to see the solution"}
plot(cars$speed, cars$dist)
```
<!-- TO CHANGE WIDGET COLOURS:
move command below out of this HTML comment area
and then re-compile;
unfilled becomes yellow, correct becomes pink
`r style_widgets("#FFFF00", "#FF3399")`
-->