```{r}
library(webexercises)
# 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
}
```5 Chapter 5
\(\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.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.
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
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.2 Version with a hyperlink in the PDF
If you want the PDF version to contain an actual hyperlink then you need to get a link to a website version of your question, like this example below. You could try and always use this link rather than download approach described above, but then you are dependent on the external website being live at all times.
{{< iframe path = "/resources/numbas/Q13/index.html",
width = "90%",
height = "900px",
url = "https://numbas.mathcentre.ac.uk/question/119566/matrix-lin\
ear-combination/embed/?token=e221efa1-3a50-429c-bb4a-8c63d2610114"
>}}
I inserted the \ symbol in the long url address, to render nicely for you, but users don’t normally see it.
When presenting the user never sees the code anyway.
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.