I would like to know if it is possible to make a comment inside an Rnw file exercise so that something can be completely ignored by both R and LaTeX. This would be useful for me to have some flexibility building exercises.
In particular, I would like to make a multiple choice question with five options where two true statements and two wrong statements are given and another statement is chosen at random between a true statements and two wrong statements. Trying to acheive this as in http://www.r-exams.org/templates/boxplots/ turns out to be quite difficult for me because all these statements involve LaTeX coding so I have problems inserting it in a R chunck. So I have tried something different, but I get an error saying that the length of exsolution and questionlist does not match. Perhaps, there is some way to make the commenting works.
<<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}
So, probably I have difficulties because I am not enough familiar with Sweave, but my problem perhaps can be addressed in multiple ways through R/exams. Any help would appreciated.
I wouldn't recommend to do this via inserting comments because (a) the code is (at least in my opinion) not so clear and (b) you are restricted to certain sampling patterns. Instead I would rather do something like this in the R code:
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]
This sets up all answers and all solutions and the samples without any restrictions five out of seven answers along with the corresponding solutions. You can then use answerlist(ans)
etc. to include the answerlist in the question.
And then it is easy to switch to any other form of sampling by replacing how i
is computed. For example, you could do this:
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
Due to the last line it is also unnecessary to set the exshuffle
tag.