Skip to contents

Creating a dynamic quiz can make it more engaging and challenging for users. The create_question_random() function in shinyquiz allows you to generate random questions. This vignette will guide you through how to use this function effectively.

The create_question_random() function takes two main arguments:
- .f: A function that generates a single random question.
- n: The number of random questions to generate for the quiz.

Creating a Random Question Generator

Before you can use create_question_random(), you’ll need to create a function that generates a single random question. This function should return a question object created by create_question() or create_question_raw().

Here’s an example that generates a random question about whether a number is even or odd:

random_question <- function() {
  number <- round(rnorm(1, 30, 10), 0)
  rand_prompt <- paste('Is', number, 'an even number?')

  q <- create_question(
    prompt = rand_prompt,
    add_choice('Yes, it is even', correct = number %% 2 == 0),
    add_choice('No, it is odd', correct = number %% 2 != 0)


You can test your function by running it a few times and verifying the preview changes each time.


Putting It All Together

Once you have your random question generator function, you can pass it to create_question_random() along with the number of questions you want to generate.

  create_question_random(.f = random_question, n = 20)

This will create a quiz with 20 randomly generated questions about whether numbers are even or odd.

Randomizing on-the-fly

Pre-Created vs. Dynamic Quizzes

When you run create_question_random() outside of a Shiny server function, the set of questions it generates is fixed at that moment. This means that if you deploy a Shiny application with this pre-created quiz, every user will see the same set of questions.

# Pre-created quiz
my_quiz <- create_quiz(
  create_question_random(.f = random_question, n = 20)

Dynamic Randomization within Shiny Server

To generate a new set of questions for each user or session, you can include create_question_random() within your Shiny app’s server function. This way, the function will re-execute each time a new user accesses the app, providing a unique set of questions.

# create static quiz for the ui side
# this is required to match the namespace
quiz <- create_quiz(
  create_question_random(.f = random_question, n = 20)

# build the shiny UI
ui <- shiny::fluidPage(
    style = "max-width: 700px",

# build the server
server <- function(input, output, session) {
  # create the dynamic quiz
  quiz <- create_quiz(
    create_question_random(.f = random_question, n = 20)
  # run the quiz server

# run the app
shiny::shinyApp(ui, server)

By placing create_question_random() inside the server function, you ensure that the quiz is dynamic, offering a different experience for each user or at different times.

Additional Information

Note that the presence of a random question in a quiz will automatically set the quiz option of sandbox = TRUE. This triggers the quiz to no longer end on the first wrong question, removes the progress bar along the top, and the grade calculation excludes any unattempted questions. You can override this behavior with set_quiz_options(override = TRUE).

You may mix and match random and regular questions. The following will create a quiz that starts with one static question and then follows with 20 random ones.

    prompt = 'My prompt explaining what the ATC of this thing should be',
    add_slider(0, 30, 15, correct = 10)
  create_question_random(.f = random_question, n = 20)

That’s it! Be sure to check out the documentation for create_question_random() for more information.