Create questions with inherit randomness. Allows one function to generate many different questions.
Arguments
- .f
a function that outputs an object of class
quizQuestion. This function can not have any arguments and must be able to produce random permutations of questions. The easiest way to ensure this is by including acreate_question()orcreate_question_raw()call inside your function (see example).- n
a numeric value indicating how many draws of function .f to include in the random question bank.
- verify_randomness
a boolean to denote if
.fhas inherit randomness. Defaults to TRUE.
Details
create_question_random() takes any user generated function .f. The function passed to the .f argument creates a random prompt along with an updated answer, the function passed to the .f argument must return an object of class quizQuestion. create_question_random() will automatically check to ensure the function passed to .f is in the appropriate format. The n argument controls how many random draws from the function passed to .f are included in the question bank for the quiz. Higher values of n allow more unique questions but extreme values of n may also lead to slower performance. To create a quiz with n randomly generated questions, create_question_random() can be passed as an argument to create_quiz().
Examples
# a function that generates a random question
random_question <- function() {
number <- round(rnorm(1, 30, 10), 0)
rand_prompt <- paste('Is', number, 'an even number?')
# using create_question inside the function helps to ensure correct class
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)
)
return(q)
}
# create a quiz with a question bank of 20 randomly generated questions
quiz <- create_quiz(
create_question_random(.f = random_question, n = 20)
)
#> ℹ Checking function input
#> ✔ Function inputs good [7ms]
#>
#> ℹ Checking function output
#> ✔ Function output good [19ms]
#>
#> ℹ Checking randomness
#> ✔ Randomness detected [56ms]
#>
#> ℹ All clear! Your random question is looking good!