gpt4 book ai didi

r - 如何使用 R 考试在 Rnw 文件中进行评论

转载 作者:行者123 更新时间:2023-12-04 10:11:28 28 4
gpt4 key购买 nike

我想知道是否可以在 Rnw 文件练习中发表评论,以便 R 和 LaTeX 可以完全忽略某些内容。这对我进行一些灵活性 build 练习很有用。

特别是,我想做一个有五个选项的多项选择题,其中给出两个正确的陈述和两个错误的陈述,并在一个正确的陈述和两个错误的陈述之间随机选择另一个陈述。试图实现这一点,如 http://www.r-exams.org/templates/boxplots/结果证明对我来说非常困难,因为所有这些语句都涉及 LaTeX 编码,所以我在将它插入 R chunck 时遇到了问题。所以我尝试了一些不同的方法,但是我收到一条错误消息,说 exsolution 和 questionlist 的长度不匹配。也许,有一些方法可以使评论起作用。

<<echo=FALSE, results=hide>>=

scelta=sample(c("%'","%'",""),size=3, replace=FALSE)
if (scelta[1]=="%'") soluz=c(1,1,rep(0,3)) else soluz=c(1,1,1,0,0)

@

\begin{question}

Say which statements are true.

\begin{answerlist}

\item first true statement
\item second true statement
\Sexpr{scelta[1]} \item third true statement

\item first wrong statment
\item second wrong statment
\Sexpr{scelta[2]} \item statment
\Sexpr{scelta[3]} \item statement

\end{answerlist}
\end{question}


\begin{solution}

<<echo=FALSE, results=tex>>=
answerlist(ifelse(soluz, "Vero", "Falso"))
@

\end{solution}


\exname{name_exercise}
\extype{mchoice}
\exsolution{\Sexpr{mchoice2string(soluz)}}
\exshuffle{5}

所以,可能我有困难,因为我对 Sweave 不够熟悉,但我的问题也许可以通过 R/考试以多种方式解决。任何帮助将不胜感激。

最佳答案

我不建议通过插入注释来执行此操作,因为 (a) 代码(至少在我看来)不太清楚,并且 (b) 您仅限于某些采样模式。相反,我宁愿在 R 代码中做这样的事情:

ans <- c(
"This is the first statement and correct.",
"This is the second statement and also correct.",
"This is the third and last correct statement.",
"This is the fourth statement and it is false.",
"This is the fifth statement, the second false one.",
"This is the sixth statement, false again.",
"This is the seventh statement, it is false and the last one."
)
sol <- rep(c(TRUE, FALSE), c(3, 4))
i <- sample(1:7, 5)
ans <- ans[i]
sol <- sol[i]

这将设置所有答案和所有解决方案以及没有任何限制的样本,七个答案中有五个以及相应的解决方案。然后您可以使用 answerlist(ans)等以在问题中包含答案列表。

然后通过替换方式很容易切换到任何其他形式的采样 i被计算。例如,你可以这样做:
i <- c(sample(1:3, 2), sample(4:7, 2)) ## two true, two false
i <- sample(setdiff(1:7, i), 1) ## one additional
i <- sample(i) ## permute

由于最后一行,也不需要设置 exshuffle标签。

关于r - 如何使用 R 考试在 Rnw 文件中进行评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61319788/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com