Search code examples
rr-exams

R/exams exams2moodle does not read data or source code


I am trying to use R/exams with an exercise that reads a local data set. The code from the exercise works fine when I run it interactively but it fails when I run it in exams2moodle(). The same issue occurs when I try to source() an R script within the exercise.

A simplified artificial example is included below.

library("exams")
exams2moodle("mysum.Rmd")

The exercise file mysum.Rmd is:

```{r, include=FALSE}
i <- sample(1:3, 1)
d <- read.csv("mydata.csv")
s <- d$x[i] + d$y[i]
```

Question
========
What is the sum of $`r i`$ + $`r i+1`$?

Meta-information
================
exname: mysum
extype: num
exsolution: `r s`

And the data file mydata.csv is:

x,y
1,5
8,3
4,4

Solution

  • The code runs fine locally because then the data file mydata.csv is in the local working directory. However, inside exams2moodle() (and all other exams2xyz() interfaces) a different temporary directory is used, in order not to clutter the user's workspace with files.

    Thus, when you want to use additional files you need to either indicate their absolute path or you need to make sure that they are copied to the same temporary directory. For the latter, there is the convenience function include_supplement() which copies files, by default taking them from the same directory that the exercise is located in. In your case you can add

    include_supplement("mydata.csv")
    

    at the beginning of the first R code chunk of your mysum.Rmd exercise (before read.csv() is applied).