I have a problem where I create a .Rmd file for an exercise and I include a large number together with the round()
function. Here is a minimal example:
```{r data generation, echo = FALSE, results = "hide"}
Value = 12000.555
```
Question
========
temp
Meta-information
================
exname: temp
extype: num
exsolution: `r round(Value, 2)`
extol: 0.01
I try to compile this exercise into an exam using exams2pdf()
yielding the following error:
exams2pdf("example.Rmd")
## Warning message: In read_metainfo(file) : NAs introduced by coercion
Why is that? I'm using R/exams version 2.3-6, and R version 3.6.3.
TL;DR: Use fmt(Value, 2)
instead of round(Value, 2)
. This avoids problems with scientific notation (and uses rounding away from zero). See ?fmt
for more details.
The reason for the error is actually not the round()
function per se, but the fact the R by default uses scientific notation for numbers with a certain number of significant digits (factory-fresh default in R is scipen = 7
). Furthermore, the knitr
package (employed in the background by R/exams) tries to format this scientific notation nicely. So instead of 12000.56
the knit()
function includes 1.200056 × 10<sup>4</sup>
in the Markdown file. You can see this when you run xweave("example.Rmd")
and then inspect the resulting example.md
file. And then the subsequent processing of the exsolution
tag hence has problems to convert this back to a number, hence the warning.
To avoid this you could increase the scipen
limit within the R code of the exercise, e.g., options(scipen = 999)
. But this is very technical and tedious. This is one of the reasons why we have written the fmt(...)
function that carries out various convenience tasks that have to do with formatting of numbers within R/exams exercises.