\(\definecolor{frog}{rgb}{1,0,0}\) \(\newcommand{\dx}{\,\text{d}x}\) \(\newcommand{\du}{\,\text{d}u}\) \(\newcommand{\dy}{\,\text{d}y}\) \(\newcommand{\dt}{\,\text{d}t}\)

5 Chapter 5

5.1 R package: webexercises

Some people are using external R packages like webexercises, they will just work out of the box. You load them as usual. I’ve added an extra function to attach accessibility aria labels to the entry boxes later.

```{r}
library(webexercises)
```
Warning: package 'webexercises' was built under R version 4.4.3
```{r}
# To allow injection of aria-labels
in_labelled_fitb <- function(answer, label_text, ...) {
  html <- webexercises::fitb(answer, ...)
  # Insert aria-label into the <input ...> tag
  html <- sub(
    "<input ",
    paste0("<input aria-label=\"", label_text, "\" "),
    html
  )
  html
}
```

The webexercises package also comes with a css and js file, so you typically need to add the following to the _metadata.yml in the folder for the file being rendered (or in the global _quarto.yml) if you want it literally everywhere.

format:
  html:
    css:
      - include/webex.css
    include-after-body: include/webex.js

Here’s an example someone may recognise.

Example 1
Consider a function taking arguments a, b and c and returning \((a+2\times b)^c\), defined as set out below.

func <-  function(a, b = 0, c = 1){
    (a + 2 * b)^c
}

Rewrite the calls below into named form (i.e. using parameter = value) and give the output of func without running it in R.

func(1, 2, 3)

This is the equivalent to and it returns the value .

5.2 Numbas embedding

Another opportunity with HTML is to embed via an iframe to some external content.

I have created an extension which makes including iframes easier.

You need to first create your Numbas question, then preview it, and click Download. This provides a zip file, which you unzip and place the folder into /resources/numbas/<your-chosen-name>. Then in the code below you just point to the index.html for the question. Questions are of varying heights and widths too, so you can customize it in when you create the iframe.

5.2.1 Plain version (no web version)

{{< iframe path = "/resources/numbas/Q13/index.html"
  width = "90%"
  height = "900px"
  >}}

5.2.3 Hiding a Numbas task

An alternative to having a large space-using question, is to hide inside a collapsed box like the following.

Task 1