如何通过R考试在Rnw文件中添加注释

问题描述 投票:0回答:1

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

特别是,我想用五个选项提出一个选择题,其中给出了两个正确的陈述和两个错误的陈述,并在一个真实的陈述和两个错误的陈述之间随机选择另一个陈述。试图像http://www.r-exams.org/templates/boxplots/中那样实现这一点对我来说非常困难,因为所有这些语句都涉及LaTeX编码,因此在将其插入R块中时遇到问题。因此,我尝试了一些不同的尝试,但是我收到一条错误消息,指出解题和问题清单的长度不匹配。也许,有一些方法可以使评论生效。

<<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 /考试以多种方式解决。任何帮助,将不胜感激。

r sweave r-exams
1个回答
0
投票

我刚刚找到一种使其工作的方法。基本上,我了解到我可以使用四倍的反斜杠逃脱。因此,在这一点上,我也可以像在箱线图示例中那样。任何意见或建议仍将受到欢迎。

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

scelta=sample(c("%","%","\\\\item"),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]} third true statement 

\item first wrong statment
\item second wrong statment 
\Sexpr{scelta[2]} third wrong statment
\Sexpr{scelta[3]} fourth wrong 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} 
© www.soinside.com 2019 - 2024. All rights reserved.