Using R script to generate answer values

10 views
Skip to first unread message

Swidbert Ott

unread,
Aug 1, 2025, 11:42:05 AMAug 1
to Numbas Users
Hi all,

I'm new to Numbas and probably getting ahead of myself ... but here is what I'm hoping to do in a nutshell: run R code (written by me, not submitted by student) to drive the answers to either numeric or SBA questions. The context is students working through a guided data analysis in their own time, and answering questions in the "exam" as they go along.

I'm failing at the first hurdle, as it were; I've tried to use a trivial bit of R to define a Numbas variable, thus:
- a "string" variable called myRcode, with my R code
- a "JME code" variable, value set to `run_code("webr", myRcode)'
- `Programming` extension enabled under "Extensions & scripts"

Admittedly, I don't even understand yet how the R output would be interfaced with the "JME code" variable, but I don't get that far because the JME code variable doesn't know about the run_code() function:
Error evaluating variable code_runner: No definition of 'run_code' of correct type found.

I'm aware of the "Code" type question part, but my understanding is this isn't what I need.

Any help appreciated - including RTFM with a pointer to the right manual; I'm struggling with https://numbas.mathcentre.ac.uk/extensions/253/documentation

Many thanks, Swidbert

Christian Lawson-Perfect

unread,
Aug 6, 2025, 11:44:06 AMAug 6
to numbas...@googlegroups.com
Hi Swidbert,
At the moment, I don't think you can use R to generate a value used in a question variable. 

R code can only be run asynchronously - you ask for the code to be run and then the result comes back at some unknown time in the future - while the variable generation code expects everything to complete synchronously. It's a limitation that I'd like to remove at some point, but it'll need a big change to how question variables are generated.

Whatever you're asking R to calculate, you'll have to do it in JME instead. If you can describe what data you need to generate, I can try to give some advice about how best to do it.

--
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, visit https://groups.google.com/d/msgid/numbas-users/e3c235f9-ab80-4b03-864b-f0f3af10a900n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages