📊R

This tutorial will guide you through all the steps to create your first R assignment in CodeGrade. For this tutorial, we will consider the example below. The zip folder contains a completed solution, a template file, and some supporting files. Please make sure to save this file somewhere accessible on your computer:

A quick note about this example In this assignment, students have been asked to create and call a function in R that reads data about a record store, calculates the total revenue and average sales price per record, and prints this as output.

Step 1: Create an assignment

Firstly, we need an assignment to work with. Whether CodeGrade is integrated into your LMS or you are using CodeGrade Standalone, you can follow the steps in the guide below to get started:

pageCreate assignment

Step 2: Configure submission settings

The first step of setting up an assignment is to ensure that students can submit their code correctly. Because this example is straightforward and only requires a single solution file, we will offer the students the option to code in the CodeGrade Editor alongside the default File Uploader option.

  1. Navigate to the Assignment management menu by using the ⚙️ icon at the right-hand side of the page header

  2. Find the Submission settings under the General tab

  3. Enable the File uploader and Editor submission methods by clicking the respective check boxes.

  4. Enabling the Editor will reveal the Template files option. Unzip Record_Store.zip and upload the analyze_record_store.r file in the Template folder.

  5. Finalize your settings by clicking the "Submit" button.

It is good practice to provide a template file when enabling the Editor so your students don't have to create their own files. This also avoids the risk of students submitting a file with the wrong name.

For more information about submission methods, see Submission Settings.

Step 3: Create a rubric

While you can always grade assignments directly by manually setting the Final grade, It's only possible to award points for your automatic tests with Rubrics. Rubrics also allow you to standardize the grading scheme for graders and clarify the grading requirements for your students.

CodeGrade offers two types of rubric categories:

  • Discrete category - Specify points in discrete steps (e.g. 0, 5, or 10 points)

  • Continuous category - Specify points as a continuous scale (e.g. 0 - 10)

  1. Navigate to the Rubric tab in the Assignment Management menu.

  2. Select Create new rubric.

  3. Create 2 new Continuous Categories. Set the parameters as follows:

Category nameDescriptionMin - Max points

IO Tests

Do you correctly calculate and print the total revenue and average sales price per record?

0 - 80

Code Structure Tests

Did you write your code in a function?

0 - 20

Step 4: Create automatic tests

Automatic tests are really what allow your students to learn uniquely. The immediate feedback they get from the automatic tests allows them to learn through trial and error by applying the instant feedback and resubmitting their work.

For this tutorial, we will set up IO and Code Structure tests.

Step 4.1: Setup

Before creating the tests, we need to ensure that we have R installed correctly and that the environment of our VM is correctly set up.

  1. Navigate to the Setup tab under the AutoTest settings.

  2. Add a Script block to your configuration and install R with the following commands:

    
    sudo apt-get update
    sudo apt-get install r-base r-base-dev
    
  3. Add an Upload Files block to your configuration and upload record_store_data.csv and locale.gen from the Test Files folder of Record_Store.zip.

  4. Add a Script block to your configuration and enter the following commands to configure the locale of the VM (This prevents warnings from R):

    
    cat >> ~/.cg_bash_env <<EOF
    export LANG=en_US.UTF-8
    export LC_ALL=en_US.UTF-8
    EOF
    
    sudo cp $UPLOADED_FILES/locale.gen /etc/locale.gen
    sudo locale-gen
    locale -a
    
  5. Navigate to the "Tests" tab of the AutoTest settings.

  6. Add a script block to your configuration. Make the dataset available to the students' programs by adding the following command:

    
    mv $UPLOADED_FILES/record_store_data.csv
    

Step 4.3: Input/Output tests

Input/Output tests, or "IO tests" for short, are a great way of checking a program's functionality by providing various input cases that should result in different outputs. You can create IO test cases in AutoTest v2 using the IO Test wrapper block and its corresponding Full match, Substring match, and Regex match blocks. For more information about these blocks, see Create an IO Test.

  1. Add a Connect rubric block to your AutoTest configuration and select the "IO Tests" rubric.

  2. Add an IO test block and nest it within the Connect rubric block. Run the student's programs with the command:

    
    Rscript analyze_record_store.R
    
  3. Add two Substring match blocks to the IO Test block. The first will test the total revenue, and the second will test the average sales price per record. Place the following values in the Expected output field in their respective Substring match block:

Total revenueAverage sales price

43902.2

25.29

It's a good idea to give your tests descriptive names so that it's clear to you and your students what is being checked (e.g., "Does analyze_record_store.R produce the correct output?").

Step 4.4: Code Structure Tests

Semgrep is a code structure testing framework that is excellent for checking if students have used a specific syntax to reach their solution. AutoTest v2 offers the Code structure test block and corresponding Positive match and Negative match blocks for running Semgrep tests. For more information about these test blocks, see Create a Code Structure Test.

For this example, we want to check that the students have created a function in their program. Here is the Semgrep rule we will be using:


rules:
  - id: untitled_rule
    pattern: $FUNC <- function(...){...}
    message: Semgrep found a match
    languages: [R]
    severity: WARNING
  1. Add a Connect rubric block to your test configuration and select the "Code structure tests" rubric category.

  2. Add a Code structure test block to your test configuration and nest it within the Connect rubric block.

  3. In the Student file input field, write "analyze_record_store.R".

  4. Add a Positive match block to your test configuration and nest it within the Code structure test block.

  5. Copy and paste the Semgrep rule in the code block above into the provided editor. Make sure to replace the template code provided.

Step 5: Test and publish your AutoTests

It's important to test your AutoTest configuration before running it on students' submissions to ensure your tests are running as expected and to check for edge cases. This is easy to do with Snapshots. When you're ready to check your tests, press the Build snapshot button at the bottom of the test block sidebar. This will make a test run of your AutoTest configuration on your Test submission.

CodeGrade will prompt you to upload a Test submission the first time you build a snapshot. Use the "Click here to upload files" option or drag-and-drop analyze_record_store.R from the Solution folder in Record_Store.zip and click "Submit".

Once you're happy with your tests, you must publish them to your students so they can run on their submissions. Publish your tests by building a snapshot, and at the bottom of the pop-up modal, click the Publish snapshot button.

Conclusion

You have just built your first automatically graded assignment, and it is ready for your students to begin submitting work!

This guide is designed to get you started with a completed assignment but doesn't go into explicit detail about all of the features CodeGrade offers. For more in-depth information about the product and the various workflows you can achieve, see Learn more or contact our support team at support@codegrade.com.

Last updated