Search code examples
r-exams

How to insert text in num_to_schoice?


I'm building on the deriv2.Rnw question from the r-exams package, which includes num_to_schoice to randomize the answer options of a schoice question:

sc <- NULL
while(is.null(sc)) {
## parameters
a <- sample(2:9, 1)
b <- sample(seq(2, 4, 0.1), 1)
c <- sample(seq(0.6, 0.9, 0.01), 1)
## solution
res <- exp(b * c) * (a * c^(a-1) + b * c^a)
## schoice
err <- c(a * c^(a-1) * exp(b * c), a * c^(a-1) * exp(b * c) + c^a * exp(b * c))
rg <- if(res < 4) c(0.5, 5.5) else res * c(0.5, 1.5)
sc <- num_to_schoice(res, wrong = err, range = rg, delta = 0.1)

My intention is to enter text in the four or five answer options, in such a way that a result similar to the one in the attached image is obtained.

enter image description here


Solution

  • You can just insert them into the answerlist() that sets up the choice options:

    answerlist(sc$questions,
      c("because Lorem ipsum dolor sit amet.",
        "because fusce et interdum neque.",
        "because suspendisse a bibendum turpis.",
        "because donec sed imperdiet justo.",
        "because duis efficitur mattis."),
      sep = ", "
    )
    

    (Note that in Rmd exercises you additionally need to add markup = "markdown".)

    However, the main problem with this in practice will be that sc$questions contains random answers and all answers are shuffled randomly. So you would have to match sc$questions to the typical errors you supplied as the wrong argument. Also you probably won't have explanations for the random answer options. So possibly it would be better to do this without num_to_schoice() and write the answer list "by hand". For automatic shuffling you can then set exshuffle to TRUE in the exercise.