Creating Exercises
Evaluate learner code, providing support with hints and solutions
The quarto-live extension allows you to define a collection of linked code cells and blocks to form an exercise. An exercise is fundamentally an interactive code block, but with the addition of optional setup code, hints, solutions, and grading.
Create an Exercise
To designate a webr block as an exercise set the exercise cell option, giving it a unique label. Later we will link additional features to the exercise by referring to this label.
Source
exercise.qmd
Fill in the blank so that the result of the sum is 10.
```{webr}
#| exercise: ex_1
1 + 2 + 3 + ______
```Output
Fill in the blank so that the result of the sum is 10.
Exercises may optionally include a series of six or more underscores, designated as “blanks”. The learner will be prompted to replace the blanks with code when evaluating an exercise.
Unlike standard interactive code blocks, exercises do not auto-evaluate by default – the “Run Code” button must be activated by the learner to evaluate the code.
Running setup code
Often you will want to run some setup code in preparation for your exercise. For example, you might want to ensure a package is available, or prepare for some data to be used in the exercise.
Designate a webr block as an exercise setup block by setting the setup: true cell option. Then, set the exercise cell option so as to match an existing exercise. The code in the exercise setup cell will be executed before every evaluation of the learner’s code.
Source
Fill in the blank so that the result of the sum is 10.
exercise.qmd
```{webr}
#| setup: true
#| exercise: ex_2
foo <- 1
bar <- 2
baz <- 3
```
```{webr}
#| exercise: ex_2
foo + bar + baz + ______
```Output
Fill in the blank so that the result of the sum is 10.
Adding hints and solutions
Add hints and solutions to your exercise in the form of fenced blocks. The blocks should set the .hint or .solution class. Then, link the blocks to your exercise by setting the exercise attribute.
Linked exercises will display UI button elements to reveal hints and solutions. Multiple hints and solutions blocks can be linked; hints will be revealed progressively and all solutions at once.
Source
exercise.qmd
Filter the `starwars` dataset so that only the droid characters are included.
```{webr}
#| setup: true
#| exercise: ex_3
library(dplyr)
```
```{webr}
#| exercise: ex_3
starwars |> ______
```
::: { .hint exercise="ex_3"}
::: { .callout-note collapse="false"}
## Hint 1
Consider using the `filter()` function from `dplyr`.
```r
starwars |> filter(______)
```
:::
:::
::: { .hint exercise="ex_3"}
::: { .callout-note collapse="false"}
## Hint 2
You should filter the dataset using the `species` column.
```r
starwars |> filter(species == ______)
```
:::
:::
::: { .solution exercise="ex_3" }
::: { .callout-tip collapse="false"}
## Fully worked solution:
Use the `filter()` function from `dplyr`:
```r
starwars |> #<1>
filter(species == "Droid") #<2>
```
1. Take the `starwars` dataset, and then,
2. Filter for the "Droid" species.
:::
:::Output
Filter the starwars dataset so that only the droid characters are included.
Consider using the filter() function from dplyr.
starwars |> filter(______)You should filter the dataset using the species column.
starwars |> filter(species == ______)Tabset exercises
You may prefer to show exercise hints and solutions in the form of a tabbed panel. This can be achieved by wrapping your fenced .hint and .solution blocks as part of a tabset panel block.
Source
exercise.qmd
:::: {.panel-tabset}
## Exercise
```{webr}
#| setup: true
#| exercise: ex_4
library(dplyr)
```
```{webr}
#| exercise: ex_4
starwars |> ______
```
## Hints
::: { .hint exercise="ex_4"}
Consider using the `filter()` function from `dplyr`.
```r
starwars |> filter(______)
```
:::
## Solution
::: { .solution exercise="ex_4" }
Use the `filter()` function from `dplyr`:
```r
starwars |> #<1>
filter(species == "Droid") #<2>
```
1. Take the `starwars` dataset, and then,
2. Filter for the "Droid" species.
:::
::::Output
Document level options
Enable and disable hints and solutions for an entire document using the live key in the document YAML header.
---
live:
show-hints: true
show-solutions: true
---