r-考试中问题结构的随机化

Randomization of question structure in r-exams

我正在尝试创建一个 canvas 测验,要求学生评估不同电路的有效电阻。这些电路包含 2 到 4 个电阻,其值选自 E12 系列。为了回答这个问题,学生们展示了他们理解电阻器之间的并联和串联连接。

对于任何单个电路配置,我都可以创建问题(这里是两个串联电阻器的示例)

    \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}

但我不知道如何随机选择问题类型。例如,如果脚本在两个串联电阻或三个串联电阻之间随机选择,则以下内容不起作用

    \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

我有点明白了,因为 if 语句被打断了,解释器对此感到窒息(我猜)。但是,有没有办法做到这一点?

是的,你是对的,这种方式的练习在句法上是不正确的,因此无法正确处理。

您需要做的是将您的 R 变量(r1char1_insert1 等)插入到 R 中的问题字符串中(例如,使用 paste()sprintf()),然后使用 \Sexpr{}.

将整个字符串插入 {question}

我在下面使用 sprintf() 对此进行了说明,其中 %s 是要插入的字符串的占位符。这将替换原始版本中的 \Sexpr{} 插入内容。

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)
}

这会生成包含问题文本的字符串 qu。然后可以通过以下方式将其包含在 {question} 中:

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