Skip to contents

Create a single quiz question. Used in conjunction with create_quiz() to generate a quiz.

Usage

create_question(
  prompt,
  ...,
  type = c("auto", "single", "multiple"),
  input = c("auto", "select", "checkbox", "radio"),
  shuffle = FALSE,
  ns = shiny::NS("quiz")
)

create_question_raw(
  prompt,
  grader,
  correct_answer_pretty,
  user_answer_prettifier = function(user_input) paste0(user_input, collapse = ", ")
)

Arguments

prompt

Text of the question prompt. Can also be an HTML element such as htmltools::div().

...

Objects of class 'quizChoice' generated from add_choice(), add_numeric(), add_slider(), or add_text(). Named options to shiny::selectInput() or shiny::checkboxGroupInput() can be passed as well.

type

One of c('auto', 'single', 'multiple'). Can the user select only one answer or multiple?

input

One of c('auto', 'select', 'checkbox'). Should the UI for a select input (shiny::selectInput()), checkbox (shiny::checkboxGroupInput()), or radio buttons (shiny::radioButtons())?

shuffle

TRUE or FALSE. If TRUE order of choices will be randomly shuffled.

ns

Namespace function generated from shiny::NS()

grader

A function that takes the user answer and determines if it is correct. Must take one argument and return TRUE or FALSE. Note that this is wrapped with purrr::possibly() and base::isTRUE() to catch any errors.

correct_answer_pretty

A string representing the correct answer that is printed 'pretty'

user_answer_prettifier

A function with one argument that takes the user's answers and prints it 'pretty'

Value

an object of class quizQuestion

an object of class quizQuestion

Details

create_question is the default method of creating quiz questions.

create_question_raw() allows any HTML in the prompt. This must contain a shiny input that is accessible via input$answers. The namespace also needs care. The default inputId is shiny::NS('quiz')('answers').

Functions

  • create_question(): Create a quiz question

  • create_question_raw(): Create a quiz question using custom inputs. This is a more flexible function that allows any html.

See also

Author

Joseph Marlo, George Perrett

Joseph Marlo

Examples

q <- create_question(
  prompt = 'My prompt explaining what the ATE of this thing should be',
  add_choice("34"),
  add_choice("59", TRUE),
  add_choice("98", TRUE)
)

q2 <- create_question(
  prompt = 'My prompt explaining what the ATC of this thing should be',
  add_slider(0, 30, 15, correct = 10)
 )
quiz <- create_quiz(q, q2)
q3 <- create_question_raw(
  prompt = htmltools::div(
    htmltools::p("my question"),
    shiny::selectInput(
      inputId = shiny::NS('quiz')('answers'),
      label = 'Select 5',
      choices = c(4, 5, 6)
    )
 ),
 grader = \(user_input) user_input == '5',
 correct_answer_pretty = '5'
)
quiz2 <- create_quiz(q3, q2)