Next to all regular Computer Science courses, CodeGrade is also very well suited for and often used in Data Science degrees. One of the most common technologies for data analysis is the programming language R, which is used in many courses taught using CodeGrade too. For instance, in the advanced Geo-information Sciences course at Wageningen University. Read their story here!
All requirements to get R to run in CodeGrade’s AutoTest are therefore automatically pre-installed, making it easy to set up autograding for your R assignments in CodeGrade. In this guide, I will go over setting this up step by step and explain how you can further customize your R automatic tests.
Setting up a basic R assignment
Setting up autograding for any R programming assignment is easy in CodeGrade. As mentioned before, R is one of the programming languages that is installed on CodeGrade by default (see full list here), this means that you do not need to do any further configuration!
We simply use Rscript to run student R scripts and then use CodeGrade’s Input / Output tests to check if the student returned the correct output. I will go over calling and grading specific functions in R and grading entire R scripts in CodeGrade. For both examples we will create automatic tests for an assignment in which a student has to print the first x numbers of the fibonacci sequence.
Autograding specific functions in R
One of the easiest ways to check R functions is using the I/O Step in CodeGrade. Using Rscript, we can call a specific function with different input arguments and check if the output matches with our expected output. If it does, we award the student with a point.
For our fibonacci assignment, our students are tasked to create a function called fibonacci that takes the length of the sequence it has to print as its only argument. In our I/O test, we can now run Rscript with some arguments:
-!- CODE language-console -!- Rscript -e "source(fibonacci.R')" -e
With the `-e` flag we can provide Rscript expressions to execute, which we now use to source the `fibonacci.R` script the student has handed in. The final `-e` is not a typo, this flag is used to now append the input argument of our input and output pair to, which is: `fibonacci(5)`. Which will print the output, which we then compare with our expected output which we set to [1] 1 1 2 3 5.
The above method is the easiest to set up, as it does not require creating a grading script and uploading that as a fixture. If you wish, you can of course write these two expressions in a simple R script, upload that as a fixture, and run it using `Rscript testFibonacci.R`. Which allows you to add more tests or upload current tests. In both cases, it is recommended to add a custom description to your test to explain to your students exactly what it is that you are grading.