Felix S
Felix S

Reputation: 1819

r-exams: R output formatting in cloze sub-item

In r-exams, we want to include R output within a cloze question sub-item; exported as PDF (for a printed exam). While printing of R output works fine in the regular question part, it fails within the \begin{answerlist} environment of the cloze sub-items.

Here's an MWE:

```{r echo=FALSE, message=FALSE, warning=FALSE}
tt <- t.test(rnorm(100), rnorm(100))
```

Question
========

Output *outside* of list environment:

```{r echo=FALSE, message=FALSE, warning=FALSE, comment=NA}
print(tt)
```

Answerlist
---------------

* Subquestion number 1.

Output *inside* of list environment:

$$\vspace*{0.1cm}$$
\
```{r echo=FALSE, message=FALSE, warning=FALSE, comment=NA}
print(tt)
```

* Subquestion number 2.


Solution
========
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 


Meta-information
================
exname: testoutput
extype: cloze
exclozetype: string|string
exsolution: x|x
expoints: 1|1

Which leads to this PDF:

enter image description here

The output loses all line breaks. Any ideas for work-arounds/solutions?

Upvotes: 3

Views: 916

Answers (1)

Achim Zeileis
Achim Zeileis

Reputation: 17183

It is not possible to include such advanced formatting within the answerlist items. Partially, this is due to some systems we can render such questions into don't support this. Hence, all text in an answerlist item is collapsed internally into a single line of text. Consequently, code or graphics etc. are not supported there.

Possible workarounds include:

  • Include all relevant output in the question itself, possibly with some numbering/itemization/etc. and then refer in the answerlist items to that. The obvious disadvantage is that content is more distributed across the question. Advantage: Technically, it works with all supported output formats.
  • If PDF is the main goal, it would also be possible to simply flag this as a single "string" question (rather than a "cloze"). The question could still include an itemized/enumerated list of subitems (possibly with custom layout, spacing, etc.). Disadvantage: R/exams does not know anything about the sub-items and hence wouldn't work as desired in Moodle output. Advantage: R/exams does not know anything about the sub-items and hence you have full control over formatting.
  • Another trick that works for PDF output is to split the question into two separate "string" .Rmd (or .Rnw) files. If processed in a common environment, the second exercise can access the data generated by the first exercise. See the discussion in the R-Forge forum: https://R-Forge.R-project.org/forum/forum.php?thread_id=33666&forum_id=4377&group_id=1337 Disadvantage: Works for PDF but not for other output like Moodle. Advantage: R/exams still knows that these are two questions.
  • For dedicated Moodle output it would also be possible to place the ##ANSWER## fields somewhere directly in the question text so that you can customize layout. I haven't got a worked "string" example but this "num" example might still be helpful: http://www.R-exams.org/templates/fourfold2/. Disadvantage: Only works for Moodle (and partially QTI) but not for PDF. Advantage: Flexible control over where user interaction elements are placed in the question text.

By conditioning on match_exams_call() it would in principle also be possible to combine some of the solutions above to achieve an exercise that works reasonably well for both PDF and Moodle, for example.

Upvotes: 3

Related Questions