Search code examples
r-exams

R/exams to create a batch of different exercises


I recently discovered the R/exams package and I found it very useful to create exercises that I use with moodle.

However, it is not clear to me how to use it to produce a given number of different exercises from a single .Rmd file. Assume that I create 4 different parameter sets to be used in 4 different exercises. Here is my data generation section:

{r data generation, echo = FALSE, results = "hide"}
data=read.csv(header=T,text="
   r,b,g,p
   .05,1.3,.04,.03
   .04,1.2,.05,.025
   .035,1.1,.02,.03
   .025,1.25,.015,.025")
i = sample(1:nrow(data),1)
dd=data[i,]

This randomly assigns a row from data to the variable dd, which is used to produce the exercise. Then, by setting the argument n in exams2moodle, I can randomly generate as many exercises as I want, of 4 different types.

However, what if instead I want to have exactly 4 exercises each using a different set? Using the option n=4 as an argument of `exams2moodle', the 4 exercises picked up at random will likely involve repetition and will not be necessarily different one from another.


Solution

  • Disclaimer: R/exams has been mainly designed for the setup where you generate large number of different exercises where it is not necessary/easy/feasible to track every possible random variation. Hence there is no built-in infrastructure to easily generate every possible variation of an exercise. I typically do not worry that some of the many variations I generate might be identical "by chance" - as long as there is still sufficient random variation.

    Workaround: However, there is the expar() function that creates temporary copies of an exercise with certain (scalar) variables fixed. See ?expar for further details. Using this you could do:

    expar("myexercise.Rmd", i = 1)
    

    This gives you a temporary file that you can use as input for all exams2xyz() generators. So you can use this with i = 1 up to i = 4 to give you all four versions of your exercise.

    Illustration: I'm using the countrycodes exercise template shipped along with the package. This also has a finite set of variable combinations from which some i is sampled (in 1, ..., 167). To get the first four of these you can do:

    ccodes <- sapply(1:4, function(ii) expar("countrycodes.Rmd", i = ii))
    exams2html(ccodes, solution = FALSE)
    

    Exam

    1. Question
      What is the three-letter country code (ISO 3166-1 alpha-3) for Angola?
    2. Question
      What is the three-letter country code (ISO 3166-1 alpha-3) for Albania?
    3. Question
      What is the three-letter country code (ISO 3166-1 alpha-3) for Argentina?
    4. Question
      What is the three-letter country code (ISO 3166-1 alpha-3) for Armenia?

    Instead of using exams2html() you can also use exams2moodle() or any other exams2xyz() interface.