2

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
Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
Nuno
  • 35
  • 6
  • I don't think that the question can be answered in this form. First, the code is rather long and requires both Portuguese and knowledge of biophysics (apparently). Second, it is not clear to me what exactly goes wrong. Does the variable `sol_a1` already have the wrong solution? Or is `sol_a1` correct but not correctly embedded in the XML file? Or is it correct in the XML but not correctly imported into Moodle? To clarify I would ask you to: Use a simpler exercise, preferably with English text, and describe concisely what you think is not correct. – Achim Zeileis Feb 02 '21 at 15:28
  • Dear Achim, I have simplified the code and translated the problem. I think I have realized that the problem is coming from the execution of a function inside the question calculation code. Apparently, the exams2moodle does not force the function to run. So it only runs if I have runned it previously and then uses calculated variables already existing in the system, giving wrong values for different parameters. Can you confirm this? How can I implement the ability to run functions and/or libraries inside questions? – Nuno Feb 03 '21 at 10:43
  • Thanks for translating the exercise and making it shorter. It's still rather long, though, and still requires some domain knowledge plus the `Script_function_prop_air.R` which we don't have. However, from your comment it seems that the problem is that not all code in the R chunks is executed successfully. So could you please try to set `knitr::opts_chunk$set(error = FALSE)` before running `exams2moodle(...)`? Does that stop the execution of the code due to an error? – Achim Zeileis Feb 05 '21 at 00:37
  • Dear Achim, Thank you for your availability. The function is a bit long, but essencially returns the air properties for a given temperature or reduced pressure. I have inserted ```knitr::opts_chunk$set(error = FALSE)``` into the code and when I run it stops and gives the error: ```Loading required namespace: rmarkdown Quitting from lines 2-38 (Ex_3-turbina_gas-test.Rmd) Error in file(file, "rt") : cannot open the connection``` – Nuno Feb 05 '21 at 10:37
  • Nuno, please boil down your example to something that is digestable for others willing to help you. This is far more complex than needed to illustrate the problem. Please make a **minimal** example, stripping off everything that is not necessary. For example, a simple .R script with a one-line function that is loaded and then used to compute something really simple in the .Rmd exercise. This does not have to be very meaningful, but could be `mysum <- function(x, y) x + y` or something like that. And then make sure that you post the precise .R and .Rmd code. – Achim Zeileis Feb 05 '21 at 20:17
  • Achim, I reduced the example to a simple question that calls a function which sums ```2*ii+1```. In the function, there is a ```read.csv``` that loads a file with numbers to sum. By running this test I realised that the problem might be caused by the ```read.csv``` inside the function. If I run the question alone, the code runs and the function loads the file. What do you think of this? – Nuno Feb 06 '21 at 18:41
  • Thanks for this. The example was still not self-contained because the data was missing. However, the `read.csv()` seems to be the crucial hint. You are trying to access a file in the local working directory - which does not work because `exams2moodle()` carries the code out in a different directory. I stripped all unnecessary details from your example, abbreviated the question, used a more precise title, and tried to answer it. If I cut too much and relevant bits are missing now, please add these again and I'll adapt my answer. – Achim Zeileis Feb 07 '21 at 00:45

2 Answers2

2

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).

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • 1
    Dear Achim, thank you very much for this. It solved the problem. Following the rational for the different temporary directory, I had to include not only the ```include_supplement("mydata.csv")```, but also the function script ```include_supplement("script_function.R")``` in the exercise code. Best regards, – Nuno Feb 08 '21 at 08:57
  • OK, thanks for the explanation. I have extended your question accordingly so that others can find this Q&A if they have the same problem. – Achim Zeileis Feb 08 '21 at 20:07
1

I had the same problem. you can also solve by specifying the complete path to your data file in the Rmd,

JPMD
  • 644
  • 1
  • 7
  • 19