简体   繁体   中英

How do I handle randomly selected graphics files in R exams for export to qti and moodle?

I have a spreadsheet of exam questions that I want to use to generate quizzes and exams using R exams , and I'd like to include graphics in some of the questions.

The template here ( http://www.r-exams.org/templates/fruit/ ) begins by defining the images as long base 64 encoded strings as generated by

base64enc::base64encode("file.png")

This seems fine, but if I have a dozen or so images of which I might only want a question to use one, two, or three images selected at random for programmatically generated exercises, how can I avoid including the encoding for all dozen images with every single exercise?

The best I can think of at the moment is to include LaTeX syntax for graphics inclusion in a spreadsheet of possible question options, and as exercises are generated, use regular expressions to find the file names inside the \\includegraphics{} commands that will be included, encode those as base 64 strings, and include them in the exercise file, but I'm wondering if there's a way I can do this without writing my own code to parse LaTeX.

First a few clarifications:

  • The fruit exercises include the images as Base64 strings because the three icons are quite small (12K per icon) and it is convenient to have all information within the Rnw/Rmd exercise without the need to store graphics files separately. It is just one trick that can be nifty and that we wanted to demonstrate.

  • For more and larger images one could do the same trick but it is probably less convenient. To illustrate how static images can be included in an exercise, the following template is available: http://www.R-exams.org/templates/Rlogo/ It uses the include_supplement() function to declare a certain file as a supplement for the exercises. If this is a graphic it can then be integrated into the exercises via \\includegraphics{...} in Rnw exercises and via ![...](...) in Rmd exercises.

  • Each exercise just has to include the supplements it actually uses (and not all files from which these were sampled). And there is no need to do the Base64 encoding manually. This is done by the exams2xyz(...) functions automatically if needed.

Now for the scenario you describe. Say you have an exercise foo.Rmd in which you want to show one of three static images foo-1.png , foo-2.png , foo-3.png and ask questions about it. Then your R code might do something like:

i <- sample(1:3, 1)
img <- paste0("foo-", i, ".png")
include_supplement(img)

which randomly selects one of the three files and declares it to be an attachment. Then within the question text you would include the image via something like:

![](`r img`)

Caveats:

  • The code above assumes that the PNG images are located in the same directory as the Rmd exercise itself. If it is in a sub-directory bar/ say, you would need include_supplement(img, dir = "bar") etc.

  • If this exercise is rendered into HTML then the original file name ( foo-1.png or foo-2.png or foo-3.png ) would be visible in the HTML source code. This may (or may not) provide a hint for students what the correct answer is. If so, it would be better to include the file with a neutral name, eg, include_supplement(img, target = "foo.png") .

  • In Rnw exercises the code for including the graphic would be something like: \\includegraphics{\\Sexpr{img}} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM