Reputation: 25323
Suppose that a question is parametrized on parameters a
and b
. And suppose that a
can take the values 1, 2 and 3, and that b
can take the values 1 and 2. Is it possible to get a version for each combination of the possible values of a
and b
? That is:
a = 1, b = 1;
a = 2, b = 1;
a = 3, b = 1;
a = 1, b = 2;
...
I mean all values for a
and b
would have to be deterministically generated and not randomly.
Upvotes: 2
Views: 77
Reputation: 17183
TL;DR You can easily generate the six fixed versions by using expar("myfile.Rmd", a = 1, b = 1)
etc. Some more details from ?expar
are provided below.
Note: However, the main motivation for that function is not to generate all versions of an exercise (although it can be used for this) but to fix some parameters in an exercise, e.g., to assure that the random variation between participants does not become too large. At least that's what I use it for. I don't mind about exactly generating all versions
Details: The expar()
function copies an exercise file (in a temporary directory) and replaces the first assignment to the specified parameter in the first code chunk. It returns the path to the temporary file with the modified exercise. This can then be processed with 'exams2xyz' "as usual". From ?expar
:
To work properly, the parameter of interest must be defined with a standard assignment in the first code chunk at the beginning of a line. The original definition of the parameter must be in a single line only (typically using something like
sample
orrunif
etc.).
Example: Based on the deriv exercise.
myderiv <- expar("deriv.Rmd", a = 1, b = 2)
myderiv
## [1] "/tmp/RtmpEnHBap/deriv+606EE82A+D5F7B.Rmd"
exams2html(myderiv)
Iteration:
If you want to iterate over all possible combinations of several parameters in your exercise, you can use expand.grid()
and sapply()
. A small convenience function for this could be:
expargrid <- function(file, ...) {
df <- expand.grid(...)
stopifnot(nrow(df) >= 1L)
sapply(1L:nrow(df), function(i) {
args <- as.list(df[i,])
args <- c(list(file = file), args)
do.call(exams::expar, args)
})
}
And then you can iterate for example:
myderivs <- expargrid("deriv.Rmd", a = 1:3, b = 1:2)
myderivs
## [1] "/tmp/RtmpfB5SQB/deriv+606F0DA0+8A2C4.Rmd"
## [2] "/tmp/RtmpfB5SQB/deriv+606F0DA0+8A838.Rmd"
## [3] "/tmp/RtmpfB5SQB/deriv+606F0DA0+8AD47.Rmd"
## [4] "/tmp/RtmpfB5SQB/deriv+606F0DA0+8B238.Rmd"
## [5] "/tmp/RtmpfB5SQB/deriv+606F0DA0+8B71D.Rmd"
## [6] "/tmp/RtmpfB5SQB/deriv+606F0DA0+8BC06.Rmd"
exams2html(myderivs)
Upvotes: 2