Search code examples
rr-exams

Randomization of question structure in r-exams


I am trying to create a canvas quiz that asks students to evaluate the effective resistance of different circuits. The circuits contain between 2 and 4 resistors with values chosen from the E12 series. To answer the question, the students demonstrate that they understand parallel and series connections between resistors.

For any single circuit configuration, I can create the question (here is an example for two resistors in series)

    \begin{question}

What is the total resistance between points a and b if $R_1 = \Sexpr{r1}$ \Sexpr{char1_insert}$\Omega$ and $R_2 = \Sexpr{r2}$ \Sexpr{char2_insert}$\Omega$?
\setkeys{Gin}{width=0.3\textwidth}
<<fig=TRUE, height = 4, width = 4, echo=FALSE, eps=FALSE, results=hide>>=
plot.new()
rasterImage(im, 0, 0, 1, xmax)
@
 \end{question}

\begin{solution}
Resistors in series should be added. So 
\begin{eqnarray*}
R_{tot} & = & R_1 + R_2\\
R_{tot} & = & \Sexpr{r1_val} + \Sexpr{r2_val}\\
R_{tot} & = & \Sexpr{res}~\Omega
\end{eqnarray*}
\end{solution}

but I cannot figure out how to randomise the choice of question type. For instance, if the script randomly chooses between two resistors in series or three resistors in series, then the following does not work

    \begin{question}
What is the total resistance between points a and b given the following:
<<>>=
if (sel==1)
{
@
$R_1 = \Sexpr{r1}$ \Sexpr{char1_insert}$\Omega$, and $R_2 = \Sexpr{r2}$ \Sexpr{char2_insert}$\Omega$, arranged as shown in the schematic
<<>>=
} else if (sel == 2)
{
@
$R_1 = \Sexpr{r1}$ \Sexpr{char1_insert}$\Omega$, $R_2 = \Sexpr{r2}$ \Sexpr{char2_insert}$\Omega$, and $R_3 = \Sexpr{r3}$ \Sexpr{char3_insert}$\Omega$ arranged as shown in the schematic
<<fig=TRUE, height = 4, width = 4, echo=FALSE, eps=FALSE, results=hide>>=
plot.new()
rasterImage(im, 0, 0, 1, xmax)
@
\end{question}
#similar code for solution

Which I kind of get because the if statements are interrupted and the interpreter chokes on that (I guess). But, is there a way to do this?


Solution

  • Yes, you are correct that the exercise in this way is syntactically incorrect and hence cannot be processed correctly.

    What you need to do is to insert your R variables (r1, char1_insert1, etc.) into a question string in R (e.g., using paste() or sprintf()) and then insert the entire string into the {question} using \Sexpr{}.

    I illustrate this below using sprintf() where %s is the placeholder for the strings that are to be inserted. This replaces the \Sexpr{} insertions from your original version.

    qu <- if(sel == 1) {
      sprintf("$R_1 = %s$ %s$\Omega$, and $R_2 = %s$ %s$\Omega$",
        r1, char1_insert, r2, char2_insert)
    } else {
      sprintf("$R_1 = %s$ %s$\Omega$, $R_2 = %s$ %s$\Omega$, and $R_3 = %s$ %s$\Omega$",
        r1, char1_insert, r2, char2_insert, r3, char3_insert)
    }
    

    This results in a character string qu that contains the question text. This can then be included in the {question} via:

    What is the total resistance between points a and b if \Sexpr{qu},
    arranged as shown in the schematic