Reputation: 43
I'm writing some code to automate as much as possible the process of generating questions. This involves building the question inside the r code part of the file. A MWE to illustrate the scenario can be the following:
```{r}
example <- paste0(
"Question\n========\n\nHello!"
, "\n##ANSWER1## ##ANSWER2##"
, "\n\nSolution\n========"
, "\n* Sol1\n* Sol2"
, "\n\nMeta-information\n================"
, "\nexname: test"
, "\nextype: cloze"
, "\nexsolution: SOL1|SOL2"
, "\nexclozetype: string|string"
)
cat("exercise:\n", example, sep='', file=stderr())
```
`r example`
After putting this code in the file "test.Rmd", the following command in RStudio:
exams2moodle("test.Rmd")
yields this warning output:
Warning messages:
1: In extract_command(x, "extype", markup = markup) :
command‘extype:’occurs more than once, last instance used
2: In extract_command(x, "exname", markup = markup) :
command‘exname:’occurs more than once, last instance used
3: In extract_command(x, "exsolution", markup = markup) :
command‘exsolution:’occurs more than once, last instance used
4: In extract_command(x, "exclozetype", markup = markup) :
command‘exclozetype:’occurs more than once, last instance used
The code works fine, as stated in the title. Although I don't fully understand the meaning of the following header (I couldn't find any documentation in the r-exams web), I am aware that putting it at the very beginning of the test.Rmd
file instead of the ```{r}
simple one removes the warning output (you can control whether warnings are shown using the echo = <BOOLEAN>
parameter):
```{r data generation, echo = FALSE, results = "hide"}
So the question is:
In addition, and related to this:
Upvotes: 1
Views: 109
Reputation: 17183
You have correctly identified the source of the problem: Your ```{r}
code chunk is not only evaluated (to create the example
variable) but it is also printed verbatim into the resulting Markdown file. And then the package is confused that extype
etc. occur twice in the document: once in the verbatim code chunk and once in the meta-information section. The former is ignored though and only the latter used, hence the exercise works correctly.
So the alternative would be to use the extended code chunk header that you indicated:
```{r data generation, echo = FALSE, results = "hide"}
This gives the code chunk a label ("data generation") which is entirely optional, suppresses the code input (echo = FALSE
which is the important bit here), and suppresses any output (which there is none of here anyway).
A shorter declaration that would also do the trick is
```{r, include = FALSE}
If you want to check what goes into the .md file after processing the .Rmd file try
xweave("test.Rmd")
and then inspect the resulting test.md
.
Having said that, the extype
etc. outside of the Meta-information
should really be ignored. Currently, this is apparently not done because some infrastructure is shared with the .Rnw reader which does not have an explicit {metainformation}
environment. I will have a look at a potential fix, though.
Upvotes: 1