Search code examples
r-exams

Extract exname information


I want create the section dynamically, not from .Rnw files. It can be done with stitle parameter of exams2moodle().

The stitle should based on directory structure and I want add the value of exname from .Rnw file.

I think should use read_metainfo which returns the value of exname too. It works well if don't use \Sexpr but if \Sexpr appears in \exsolution{...} drop an error:

Error in string2num(exsolution) :
  all numeric items must be finite and non-missing
Calls: read_exercise -> read_metainfo -> string2num
In addition: Warning message:
In string2num(exsolution) : NAs introduced by coercion
Execution halted

The foo.Rnw file:

<<echo=FALSE, results=hide>>=
solution <- 10
@
\exname{foo}
\exsection{bar}
\extype{num}
\exsolution{\Sexpr{solution}}
\begin{question}
  Foo text...
\end{question}

The read_metainfo("foo.Rnw") fails. Do I something wrong? Should I try another way?

EDIT: after update of exams only appears the warning message and doesn't halt.


Solution

  • As conceptually everything in the exercise files could be generated dynamically, it is only possible to read the exercise after weaving it. This is because not only the content of the metainformation tags but even the entire tags could be generated by the code in the exercise.

    The workhorse function that performs the weave, read, transform, and write steps underlying all exams2xyz() interfaces is xexams(). It defaults to just weaving (via xweave()) and reading (via read_metainfo()) an exercise without any transformation or writing. Thus you can do:

    xexams("foo.Rnw")[[1]][[1]]$metainfo
    

    The indexing is needed to select the first random replication of the first exercise, and then extracting the metainformation.