Skip to contents

The moodlequiz R package which allows the creation of Moodle quiz questions using literate programming with R Markdown. This makes it easy to quickly create a quiz that can be randomly replicated with new datasets, questions, and options for answers.

Installation

You can install the development version of moodlequiz like so:

remotes::install_github("numbats/moodlequiz")

Example

Below is a Moodle quiz where the students have to select the right variables from the data to map onto the plot aesthetics in ggplot2.

The above quiz is created from the R Markdown document below. Knitting the document below will generate 5 different versions of the quiz where the x, y, color, and size are mapped randomly to one of the variables in the mtcars data.

---
output: 
  moodlequiz::moodlequiz:
    replicates: 5
title: Drawing a scatterplot
times: 5
moodlequiz:
  category: datavis-scatterplots
---

# Data

```{r setup, include = FALSE}
library(tidyverse)
library(rlang)
library(moodlequiz)
knitr::opts_chunk$set(echo = FALSE,
                      results = "hide",
                      fig.height = 4, 
                      fig.width = 5,
                      fig.path = "",
                      fig.cap = "",
                      fig.align = "center")
```

```{r data}
cols <- colnames(mtcars)
cats <- c("cyl", "vs", "am", "gear", "carb")
nums <- setdiff(cols, cats)
x <- sample(nums, 1)
y <- sample(setdiff(nums, x), 1)
color <- sample(cats, 1)
size <- sample(setdiff(nums, c(x, y)), 1)
```

You have been asked to analyse the `mtcars` data. The variables and the class types of the data is shown below.

```{r, echo = TRUE, results = "show"}
str(mtcars)
```

# Plot

As a starting point, you decide to draw a scatter plot for some variables. Complete the code below to get the target plot below:

```r
ggplot(mtcars, aes(x = `r cloze(x, cols)`, 
                   y = `r cloze(y, cols)`,
                   color = factor(`r cloze(color, cols)`),
                   size = `r cloze(size, cols)`)) +
    `r cloze("geom_point", ls(envir = as.environment("package:ggplot2"), pattern = "^geom_"))`()
```

```{r, results = "show"}
ggplot(mtcars, aes(!!sym(x), !!sym(y), size = !!sym(size), color = factor(!!sym(color)))) + 
  geom_point() +
  theme(plot.background = element_rect(color = "black"))
```