Search code examples
rr-exams

How to add existing images to R-exams questions


In RMarkdown I usually use somewhat like

```{r}
knitr::include_graphics("myimage.png")
```

to add already existing images to the output, but this doesn't work via exams2xyz().
fig.path = "", fig.caption = "" does not fix the problem.
The classic Markdown way ![alt text](figures/img.png)doesn't work, too.

Any ideas, where I'm wrong?

EDIT: Thank's to @jaySF I'm now aware of an working directory issue, but still face the challange to embed the figures into (e.g.) the .xml-questions for moodle import.


Solution

  • TL;DR See the worked example in the Rlogo.Rmd exercises provided within the package: exams2html("Rlogo.Rmd") and http://www.R-exams.org/templates/Rlogo/

    Details: The exams package (more specifically the xexams() workhorse function) do all the handling of the temporary directories. The exercises templates (.Rmd or .Rnw) are copied to a temporary directory, weaved/knitted there, read into R, and then the output file(s) are produced in some output directory. That's why some strategies from knitr don't work for .Rmd exams - or they just work in certain situations, e.g., when specifying full paths (rather than relative paths).

    Solution: To make exams recognize a certain existing file (not necessarily just graphics) as a supplement to a certain exercise, the .Rmd file should simply copy that file to the current directory when the file is weaved/knitted. The convenience function include_supplement() does exactly that and additionally searches certain directories (specifically edir, if specified) for the files. Subsequently, the copied file "foo.ext" needs to be included in the exercise, e.g., via ![](foo.ext) for a graphic or [foo.ext](foo.ext) for a hyperlink.