# automatically create a bib database for R packages # add any packages you want to cite here knitr::write_bib(c( .packages(), 'bookdown', 'webexercises' ), 'packages.bib')

# Uncomment to change widget colours: #style_widgets(incorrect = "goldenrod", correct = "purple", highlight = "firebrick")

This is a Web Exercise template created by the #PsyTeachR team at the University of Glasgow, based on ideas from Software Carpentry. This template shows how instructors can easily create interactive web documents that students can use in self-guided learning.

The `{webexercises}`

package provides a number of functions that you use in inline R code or through code chunk options to create HTML widgets (text boxes, pull down menus, buttons that reveal hidden content). Examples are given next. Render the book to see how they work.

**NOTE: To use the widgets in the compiled HTML files, you need to have a JavaScript-enabled browser.**

These functions are optimised to be used with inline r code, but you can also use them in code chunks by setting the chunk option `results = 'asis'`

and using `cat()`

to display the result of the widget.

# echo = FALSE, results = 'asis' opts <- c("install.package", "install.packages", answer = "library", "libraries") q1 <- mcq(opts) cat("What function loads a package that is already on your computer?", q1)

`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.

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)`

`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

`torf()`

)- True or False? You can permute values in a vector using
`sample()`

.`r torf(TRUE)`

`longmcq()`

)When your answers are very long, sometimes a drop-down select box gets formatted oddly. You can use `longmcq()`

to deal with this. Since the answers are long, It's probably best to set up the options inside an R chunk with `echo=FALSE`

.

**What is a p-value?**

opts_p <- c( "the probability that the null hypothesis is true", answer = "the probability of the observed, or more extreme, data, under the assumption that the null-hypothesis is true", "the probability of making an error in your conclusion" )

`r longmcq(opts_p)`

**What is true about a 95% confidence interval of the mean?**

# use sample() to randomise the order opts_ci <- sample(c( answer = "if you repeated the process many times, 95% of intervals calculated in this way contain the true mean", "there is a 95% probability that the true mean lies within this range", "95% of the data fall within this range" ))

`r longmcq(opts_ci)`

Create sections with the class `webex-check`

to add a button that hides feedback until it is pressed. Add the class `webex-box`

to draw a box around the section (or use your own styles).

::: {.webex-check .webex-box}

I am going to learn a lot: `r torf(TRUE)`

opts <- c( "the probability that the null hypothesis is true", answer = "the probability of the observed, or more extreme, data, under the assumption that the null-hypothesis is true", "the probability of making an error in your conclusion" ) cat("What is a p-value?", longmcq(opts))

:::

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.

**Recreate the scatterplot below, using the built-in cars dataset.**

with(cars, plot(speed, dist))

`r hide("I need a hint")`

See the documentation for `plot()`

(`?plot`

)

`r unhide()`

plot(cars$speed, cars$dist)

**Any scripts or data that you put into this service are public.**

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.