Improved r code generation

Generated R code is much shorter. PHP code hopefully more readable.

Please make sure to test with several exams. I only had one (from Sharon).

Also I was not sure about the handling of null values from the database, but I don't think the code has changed that much.