moodler-exams

Static images not being included in R/exams output


I'm trying to generate Moodle exercises within R using the exams package, and am trying to include some static images in my exercises with the help of the exams package's include_supplement function. At the moment, I can't get my images to be included in either the resulting Moodle XML file or the HTML file generated by the exams2moodle and exams2html functions.

Below is a MWE Rmd file for my exercises with static images included.

``` {r}
exams::include_supplement("Saving R Script File.png",
  dir="C:/Users/fowal44p/OneDrive - University of Otago/0OLAF Transition/R-Exams/STAT110 and STAT115 questions/STAT110 2025/A1 and Lab 1/Q2")
```

Question
============
Trying to get the image to appear using markdown syntax

![Caption](Saving R Script File.png)

Answerlist
--------------
* Enter the number 1

Meta-information
=====================
extype: num
exsolution: 1
extol:0
extitle:A test qn

In exams2pdf the image is included correctly but not it exams2html or exams2moodle. Instead the caption is displayed twice.

Note that the static image I want to include is in the same folder as the question Rmd file.


Solution

  • Source of the problem:

    The problem is due to the spaces in the file name Saving R Script File.png of your static image. Some of the HTML converters employed in R/exams preserve these as they are while others encode them as Saving%20R%20Script%20File.png. In the latter case the code that prepares everything for the output format does not recognize that this is in fact the same as the original file name. Hence the image is not correctly embedded into the HTML output.

    Workaround:

    As the code that does the embedding does not know anymore which converter was used, it is best to avoid the problem by using file names without spaces. So the easiest solution would be to rename the original static file. If that is not easy to do, then use include_supplement(..., target = "...") to just rename the copy in the exercise.

    Example:

    In the Rmd from your MWE you could do:

    ``` {r, include=FALSE}
    exams::include_supplement("Saving R Script File.png", target = "image.png")
    ```
    
    Question
    ========
    Trying to get the image to appear using markdown syntax
    
    ![](image.png)
    
    Enter the number 1
    
    Meta-information
    ================
    extype: num
    exsolution: 1
    extol: 0
    extitle: A test qn
    

    In addition to the renaming of the image file I have done the following: