Typesetting correct answer/prompt in (R) code type

11 views
Skip to first unread message

svda...@gmail.com

unread,
Sep 14, 2022, 8:19:18 AM9/14/22
to Numbas Users
Hi all,

I am finding the R code answer type very useful. But I have hit one 'feature' that I would like some advice on. 

In the 'Correct answer' box, I often need to use curly braces. E.g. when the correct code involves a for loop or an if...else. If I put the correct R code straight into the Correct answer box I get an error, because it thinks that inside the curly braces should be variables, and these are missing. (If I define the variables, it tries to evaluate the expression, which is also not the right thing to do here.)

Example: this is not accepted as a Correct answer:

  s <- 0
  for (j in 1:n) {
   s <- s + (2*j-1)
  }

My workaround is to define the entire model code answer as a safe string in a variable. But this is quite cumbersome when the model answer is many lines long. Example: a variable defined as follows is accepted and will print fine.

  safe("s <- 0 \nfor (j in 1:n) {\n s <- s + (2*j-1)\n}")

I hit the same problem in the Prompt text box for each question, although in that case it seems to work ok as long as I include line breaks in exactly the right places. 

Is there an alternative, e.g. some way to escape the usual function of curly braces (e.g. \{ ... \}) inside the Correct answer nd Prompt boxes?

Keep up the good work!

All the best,
Simon

Christian Lawson-Perfect

unread,
Sep 22, 2022, 10:11:43 AM9/22/22
to numbas...@googlegroups.com
I've added a setting "substitute variables into the correct answer?", which is turned on by default, preserving the existing behaviour.
If you turn it off, then the code in the "Correct answer" field is displayed exactly as you wrote it, so the curly braces in your example would be preserved.

For other situations where you want a long string of text containing curly braces, you can use the "Long plain text string" data type in the Variables tab: this produces a safe() value by default, so variable substitution doesn't happen unless you pass it through the render() function.

--
You received this message because you are subscribed to the Google Groups "Numbas Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numbas-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/numbas-users/51764ebe-a308-45d6-a927-c7b1c6f1e14en%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages