library(learnr) knitr::opts_chunk$set(echo = FALSE)
# CHECK: Will emit lifecycle message during render good_value <- function(value) { if (!grepl("na", value, ignore.case = TRUE)) { mark_as(correct = TRUE, "Nice!") } else { mark_as(correct = FALSE, "nanananana hey hey good bye") } } question_text( "Write something that doesn't say 'na'.", answer("na", correct = FALSE, message = "But why?"), answer("banana", FALSE, "Are you for real?"), answer_fn(good_value), allow_retry = TRUE, random_answer_order = TRUE )
10
or 52
to get custom incorrect messagequestion_numeric( "What's a reasonably large number?", answer("10", message = "Order of magnitude off"), answer("52", message = "Off by about a dozen"), answer_fn(~ { if (.x == 42) { mark_as(TRUE, "That's perfect, it's the meaning of life!") } else if (.x < 50) { mark_as(FALSE, "Think bigger...") } else if (.x < 75) { mark_as(FALSE, "Biggish, but think bigger...") } else { mark_as(TRUE, "Hey, that's reasonably large!") } }), allow_retry = TRUE, options = list(tolerance = 2) )
tomato
, mozarrella
, basil
to get "Close enough..."bacon
to the above to get "We'll allow bacon."pepperoni
to get "Great topping!"tomato
, mozarrella
, basil
, EVOO
to get fully correct (no custom message)question_checkbox( "Select all the toppings that belong on a Margherita Pizza:", answer("tomato", correct = TRUE), answer("mozzarella", correct = TRUE), answer("basil", correct = TRUE), answer("extra virgin olive oil", correct = TRUE), answer("pepperoni", message = "Great topping! ... just not on a Margherita Pizza"), answer("onions"), answer("bacon"), answer("spinach"), answer_fn(~ if ("spinach" %in% .x) incorrect("yuck, spinach!")), answer_fn(function(value) { value <- sort(value) almost <- c("tomato", "mozzarella", "basil") if (identical(sort(almost), value)) { mark_as(TRUE, "Close enough, but a little EVOO would make it even better.") } if (identical(sort(c("bacon", almost)), value)) { mark_as(TRUE, "You know what? We'll allow bacon") } }), random_answer_order = TRUE, allow_retry = TRUE, try_again = "Be sure to select all four toppings!" )
Not supported, emits a warning during render and answer function answers are dropped.
question_radio( "Pick the letter B", answer("A"), answer("B", correct = TRUE), answer("C", correct = TRUE), answer("D"), answer_fn(function(value) value %in% c("C", "D")), allow_retry = TRUE, random_answer_order = TRUE )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.