5

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.

sammerk
  • 1,143
  • 1
  • 9
  • 23
  • You probably have a mess with your working directory. Check `getwd()`. E.g. `figures/img.png` yields sth. like `C:/YOURWD/figures/img.png`. Does that help? – jay.sf Jun 19 '18 at 10:46

1 Answers1

6

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.

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • Thanks Achim - really sophisticated approach (all the more in my lay eyes)! As it works with `exams2arsnova()`, too it additionally prevents me from annoying image uploads to flickr etc. and embedding them into the questions. – sammerk Jun 20 '18 at 04:43
  • Yes, exactly. By default the supplementary file/graphic is simply included in each resulting exercise via Base64 encoding. Of course, if you have large static files, it would be better to upload them else (e.g., flickr) and include only the hyperlink. But for smaller supplements the convenience and robustness of including it everywhere justifies the additional storage requirements. – Achim Zeileis Jun 20 '18 at 09:22
  • It doesn't work with `exam2moodle()` though. If I include an image in Rmd with ![](/image.png) The image is displayed in moodle with a generic placeholder and the link to the image is //image.png. What am I doing wrong? – Mat D. Jun 17 '21 at 12:43
  • Don't specify the full path to image in the markdown code. Instead use `![](image.png)` and use `include_supplement("/image.png")` in an R chunk in the exercise. The full path can even be omitted if it is the same as the directory of the exercise. _[Note: This is actually what my answer above tries to point out. Maybe this is clearer if you re-read it now?]_ – Achim Zeileis Jun 17 '21 at 15:49