Setting up R with Jupyter Notebooks

Pavel Dimens Updated by Pavel Dimens

Installing R

Installing R in Codio is a matter of using apt to pull the right packages. Before you start, you should do a cursory sudo apt update && sudo apt upgrade. These boxes don't get updated often, so it might take a few minutes.

Ubuntu 18.xx
  1. Add the GPG key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
  1. Add the R repository
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/'
  1. Install R and build-essential
sudo apt update && sudo apt install r-base build-essential
Ubuntu 21.xx
  1. Install dependencies if necessary
sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
  1. Add the GPG key and repository
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
  1. Install R and build-essential
sudo apt update && sudo apt install r-base build-essential
The extra dependencies - Important!

You'll need a few more dependencies for the languageserver etc. to build. Better to take care of that ahead of time instead of wasting time with failing builds.

sudo apt install libxml2-dev libssl-dev libcurl4-openssl-dev

Installing the R packages

For simplicity, do this inside a terminal R console, activating it as the root user

sudo R

After you're greeting with the standard R welcome message, you will want to install 3 specific packages: IRkernel, languageserver, testthat.

install.packages(c("IRkernel", "languageserver", "testthat"))

It'll take several minutes, but once that's done, you need to install the R Jupyter kernel with

IRkernel::installspec()

Now, when you enter a Jupyter notebook, you can select R as one of the available kernels. The languageserver takes care of syntax highlighting and tab autocompletion. testthat is what you will use for autograding tests.

Setting up autograding

Assuming nbgrader is installed, you will need to create (or edit an existing) .codio-jupyter file in the main Codio stack directory.

.codio-jupyter config

This is the preferred way of setting this up, as opposed to creating an nbgrader_config.py file. The contents of .codio-jupyter must contain:

nbgrader:
ClearSolutions.begin_solution_delimeter:
BEGIN SOLUTION
ClearSolutions.end_solution_delimeter:
END SOLUTION
ClearSolutions.code_stub:
R: |
# YOUR ANSWER HERE
python: |
# YOUR ANSWER HERE
raise NotImplementedError()

The most imporant declaration is ClearSolutions.code_stub: R, which declares how to handle solutions in the R kernel.You have some wiggle room as to what you want the "code stub" text to be. Changing the begin/end solution delimeter is not recommended because it's configured for the postgrader (discussed next).

Including postgrader.py

This file is discussed in another HelpDocs article, but is included here for completeness. This file needs to be put into .guides/secure/postgrader.py. Don't change anything about this file, unless you have a very good reason to do so.

#!/usr/bin/env python3
import sys
import re
START = '<span class="c1">### BEGIN HIDDEN TESTS</span>'
END = '<span class="c1">### END HIDDEN TESTS</span>'
html_path = sys.argv[1].rstrip()
with open(html_path, 'r') as content_file:
content = content_file.read()

def replaceTextBetween(originalText, delimiterA, delimiterB, replacementText):
index_from = 0
index_to = len(originalText)
if delimiterA in originalText:
index_from = originalText.index(delimiterA)

if delimiterB in originalText:
index_to = originalText.index(delimiterB) + len(delimiterB)

return originalText[0:index_from] + originalText[index_to:]

while START in content:
content = replaceTextBetween(content, START, END, '')
with open(html_path, 'w+') as stream:
stream.write(content)

What's important to note is the START and END strings. This is what governs hiding the tests, which are expected to between a block like so:

### BEGIN HIDDEN TEST
line of code
line of code
etc.
some kind of test
### END HIDDEN TEST

More information on configuring autograding in Jupyter within Codio can be found here.

Using testthat for grading

The R package testthat will handle the unit tests that will do the grading. The documentation for it is here. It displays some startup messages, so a good rule of thumb is to begin your tests by supressing that. Here is an example of an autograded task:

Task:

Create a new vector v1 that contains 3 elements

## BEGIN SOLUTION
v1 <- c(1,2,3)
## END SOLUTION

which gets converted to the student version upon Codio publishing:

# YOUR ANSWER HERE

And the corresponding test:

### BEGIN HIDDEN TEST
suppressPackageStartupMessages(require(testthat))
expect_equal(length(v1),3)
print("Success!")
### END HIDDEN TEST

How did we do?

Adding Extensions to Jupyter Notebooks

Change Jupyter Notebook Auto Save Interval

Contact