Search code examples
r-exams

Multiple plots in repeated question yields "Error in exm[[dups[j]]] : subscript out of bounds"


When creating worksheets with exams2pdf() from R/exams, I like to repeat an exercise file multiple times to yield different numbers. However, when I include two plots in an exercise (e.g., one in the question and one in the solution) this yields:

Error in exm[[dups[j]]] : subscript out of bounds

A reproducible example is included below. It works with one plot, and it works if I don't repeat the question. Also, the problem can be avoided by making multiple copies of a simple.Rmd (say simple1.Rmd and simple2.Rmd with different chunk names in each copy) but it seems there should be a better way.

The Rmd file: simple.Rmd

Question
========
A question.
```{r drawit}
x = (-330):330/100
y = dnorm(x)
plot(x,y)
```

Solution
========
Let's redraw...
```{r drawagain}
x2 = (-330):330/100+100
y2 = dnorm(x2,mean=100,sd=1)
plot(x2,y2)
```

Meta-information
============
extype: num
exsolution: 10
exname: calc

And the replication R code:

library("exams")
q1 = "simple.Rmd"
probs = c(q1,q1)
exams2pdf(probs)

The Rmd file will knit fine (with two plots) but running the code above yields the above mentioned

Error in exm[[dups[j]]] : subscript out of bounds

Solution

  • Thanks for reporting this, this is a bug in exams2pdf()! Single duplicated supplement names were already corrected but the case of multiple duplicated supplement names was not. I've just committed a fix to the repository on R-Forge that addresses the issue.

    It would be great if you could install the development version of the package from R-Forge to test whether the fix also works correctly on your real use-case. You can install from within R via:

    install.packages("exams", repos="http://R-Forge.R-project.org")