Question Authors' Forum

Programming statistics in R

Picture of Chris Sangwin
Programming statistics in R
by Chris Sangwin - Wednesday, 23 November 2016, 1:13 AM

Is there any support for programming statistics in R?  I've had some interest from statisticians here about this possibility.

If not then how much work would this be to add?



Picture of Jenny Harlow
Re: Programming statistics in R
by Jenny Harlow - Wednesday, 23 November 2016, 6:00 AM

I asked about this and we did a very little investigation about a year ago but then I ran out of free time and got distracted ...  We started by looking at a basic R install to see what configuration options would be useful.  I do remember noting down the --silent option.  There did seem to be potential for creating some resources that our stats students who need to use R but are not programmers could use to at least start finding their feet with it.


Picture of Richard Lobb
Re: Programming statistics in R
by Richard Lobb - Wednesday, 23 November 2016, 11:47 AM

Just to give you a bit of background to Jenny's answer ...

When Jenny raised the question of R a year or two ago, my first response was that it would be trivial to ask R questions of the simple "write-an-R-function" or "write-an-R-program" variety. I installed R-base on our Jobe server (5 mins work) and we used a Python3 question (with R as the Ace language) to run R. That was easy enough. Here's a possible template:

import subprocess
r_prog = """{{ STUDENT_ANSWER | e('py') }}"""
r_prog += "\n" + """{{TEST.testcode | e('py')}}"""
with open('prog.r', 'w') as fout:

cmd = "R --slave --vanilla", stdin=open('prog.r'), universal_newlines=True)

With that question type you can ask questions like the following:

Screen shot of simple R question

However, as I recall that wasn't the sort of question that Jenny thought the stats lecturers would want to ask their students, who aren't really programmers. So then you get into the much harder issue of "What sort of question to you really want to ask?".


Picture of Chris Sangwin
Re: Programming statistics in R
by Chris Sangwin - Wednesday, 23 November 2016, 10:10 PM

Thank you both for such swift and helpful replies.

I'm very reassured that CodeRunner can, at least at a technical level, accept R code.  I'm not sure exactly what my colleagues have in mind. We will be running on a server which also has my own STACK question type installed ( and I think a combination of the normal Moodle questions, STACK and CodeRunner will be an interesting combination of tasks for students which combine mathematical and programming elements.

The 10^6$ question is always "What sort of question to you really want to ask?".  I'll talk with colleagues about that one...