Search code examples
rr-exams

Why does exams2nops seem to run the markdown code twice the first time around?


I'm trying to put a counter into an exercise that counts how often I use it when generating exams, but I'm noticing that when I run exams2nops, the first time seems to always be counted twice. Some toying around seems to suggest that the code of the .Rmd file seems to simply be run twice the first time around.

This is the code of NopsProblem.Rmd

```{r data generation, echo = FALSE, results = "hide"}

envir <- .GlobalEnv
if (length(envir$N)==0) envir$N <- envir$num <- 0
envir$num <- envir$num + 1

```

Question
========

`r num`

Meta-information
================
extype: num
exsolution: 1
exname: problem

The idea is that the first time this is run, the counter is set to 0 and then immediately updated to 1. Each subsequent call should increase the counter by 1. When I run exams2pdf(c("NopsProblem.Rmd","NopsProblem.Rmd","NopsProblem.Rmd")), this works without problem. However, if I run exams2nops(c("NopsProblem.Rmd","NopsProblem.Rmd","NopsProblem.Rmd")), the counter starts at 2 for the first question. Is this a bug or am I missing something?


Solution

  • Your observation is correct and no bug.

    • Internally, exams2nops() calls exams2pdf() but with a custom template. For this template, various pieces of information are required, e.g., the number of choice alternatives per question or the number of open-ended questions, etc.
    • The only way to obtain these pieces of information is to knit/weave all exercises once and extract the resulting meta-information (and assume that the type of question and number of choice alternatives does not vary across random replications).
    • In order to assure that exams2nops() and exams2pdf() yield the same randomly-generated exercises, the random seed is restored after the first initial run. Also, the first run is performed in a separate environment so that the results do not affect the results of subsequent runs.

    What exactly are you trying to compute? I expect that there would be better options for this rather than storing stuff in the global environment.