knitr::opts_chunk$set(echo = TRUE) if (!requireNamespace("webex")) { stop("You must have the 'webex' package installed to knit HTML from this template.\n devtools::install_github(\"psyteachr/webex\")") } else { library("webex") }
This is a Web Exercise template created by the psychology teaching 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 webex package provides a number of functions that you use in inline R code to create HTML widgets (text boxes, pull down menus, buttons that reveal hidden content). Examples are given below. Knit this file to HTML to see how it works.
NOTE: To use the widgets in the compiled HTML file, you need to have a JavaScript-enabled browser.
total_correct()
)The function total_correct()
displays a running total of correct responses. Change the elem
argument to display in a different style (e.g., h2
or h3
for header styles). If you're comfortable with css styles or classes, you can add them with the args
argument.
r total_correct(elem = "h3", args = "style='color:#003366;'")
fitb()
)Create fill-in-the-blank questions using fitb()
, providing the answer as the first argument.
r fitb("4")
You can also create these questions dynamically, using variables from your R session.
x <- sample(2:8, 1)
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
.
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.
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.
r fitb(c("A", "E", "I", "O" , "U"), ignore_case = TRUE)
You can use regular expressions to test answers against more complex rules.
r fitb("^[a-zA-Z]{3}$", width = 3, regex = TRUE)
mcq()
)r mcq(c("let you go", "turn you down", "run away", answer = "let you down"))
"r mcq(c(answer = "bless the rains", "guess it rains", "sense the rain"))
down in Africa" -Tototorf()
)sample()
. r torf(TRUE)
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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.