library(learnr)
knitr::opts_chunk$set(echo = FALSE)

Answers

Text

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

Numeric

  1. Pick within 2 of 10 or 52 to get custom incorrect message
  2. Pick anything below 50 (except 10, 42, 52) to get "think bigger"
  3. Pick anything less than 75 (except 10, 42, 52) to get "Biggish"
  4. Pick 42 to get "meaning of life" correct
  5. Pick anything above 75 to get "reasonably large!"
question_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)
)

Checkbox

  1. Verify that the options shuffle when restarting the tutorial.
  2. Including "spinach" always returns just yuck, spinach! no matter what options are selected.
  3. Answer tomato, mozarrella, basil to get "Close enough..."
  4. Add bacon to the above to get "We'll allow bacon."
  5. Try a wrong combination not including the above to get incorrect messages.
  6. Include pepperoni to get "Great topping!"
  7. Choose 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!"
)

Radio

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
)


rstudio/learnr documentation built on Sept. 6, 2024, 11:06 p.m.