Set up a lab to teach R on Linux

Note

This article references features available since the April 2022 Update (preview), in which lab plans replaced lab accounts. For more information, see What's New in the April 2022 Update (preview).

R is an open-source language used for statistical computing and graphics. It's used in the statistical analysis of genetics to natural language processing to analyzing financial data. R provides an interactive command line experience. RStudio is an interactive development environment (IDE) available for the R language. The free version provides code editing tools, an integrated debugging experience, and package development tools.

This article will focus on solely RStudio and R as a building block for a class that requires the use of statistical computing. The deep learning and Python and Jupyter Notebooks class types setup RStudio differently. Each article describes how to use the Data Science Virtual Machine for Linux (Ubuntu) marketplace image, which has many data science related tools, including RStudio, pre-installed.

Lab configuration

To set up this lab, you need an Azure subscription and lab plan to get started. If you don't have an Azure subscription, create a free account before you begin.

External resource configuration

Some classes require files, such as large data files, to be stored externally. See use external file storage in Azure Lab Services for options and setup instructions.

If you choose to have a shared R Server for the students, the server should be set up before the lab is created. For more information on how to set up a shared server, see how to create a lab with a shared resource in Azure Lab Services. For instructions to create an RStudio Server, see Download RStudio Server for Debian & Ubuntu and Accessing RStudio Server Open-Source.

If you choose to use any external resources, you’ll need to Connect to your virtual network in Azure Lab Services with your lab plan

Important

Advanced networking must be enabled during the creation of your lab plan. It can't be added later.

Lab plan settings

Once you get have Azure subscription, you can create a new lab plan in Azure Lab Services. For more information about creating a new lab plan, see the tutorial on how to set up a lab plan. You can also use an existing lab plan.

Enable your lab plan settings as described in the following table. For more information about how to enable Azure Marketplace images, see Specify the Azure Marketplace images available to lab creators.

Lab plan setting Instructions
Marketplace images Enable Ubuntu Server 18.04 LTS image.

Lab settings

For instructions on how to create a lab, see Tutorial: Set up a lab. Use the following settings when creating the lab.

Lab setting Value and description
Virtual Machine Size Small GPU (Compute)
VM image Ubuntu Server 18.04 LTS
Enable remote desktop connection This setting should be enabled if you choose to use RDP. This setting isn't needed if you choose X2Go to connect to lab machines.

If you choose to instead use RDP, you will need to connect to the Linux VM using SSH and install the RDP and GUI packages before publishing the lab. Then, students can connect to the Linux VM using RDP later. For more information, see Enable graphical remote desktop for Linux VMs.

Template configuration

After the template machine is created, start the machine, and connect to it to install R, RStudio Desktop and optionally X2Go Server.

First, let’s update apt and upgrade existing packages on the machine.

sudo apt update 
sudo apt upgrade

Install X2Go Server

If you choose to use X2Go, install the server. You'll first need to Connect to a Linux lab VM using SSH to install the server component. Once that is completed, the rest of the setup can be completed after connecting using the X2Go client.

The default installation of X2Go isn't compatible with RStudio. To work around this issue, update the x2goagent options file.

  1. Edit /etc/x2go/x2goagent.options file. Don’t forget to edit file as sudo.

    1. Uncomment the line that states: X2GO_NXAGENT_DEFAULT_OPTIONS+=" -extension GLX"
    2. Comment the line that states: X2GO_NXAGENT_DEFAULT_OPTIONS+=" -extension GLX"
  2. Restart the X2Go server so the new options are used.

    sudo systemctl restart x2goserver
    

Alternatively, you can build the required libraries by following instructions at Glx Xlib workaround for X2Go.

Install R

There are a few ways to install R on the VM. We'll install R from the Comprehensive R Archive Network (CRAN) repository. It provides the most up-to-date versions of R. Once this repository is added to our machine, we can install R and many other related packages.

We need to add the CRAN repository. Commands are modified from instructions available at Ubuntu Packages for R brief instructions.

#download helper packages
sudo apt install --no-install-recommends software-properties-common dirmngr
# download and add the signing key (by Michael Rutter) for these repos
sudo wget -q "https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc" -O /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
#add repository
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/"

Now we can install R, running the following command:

sudo apt install r-base

Install RStudio

Now that we have R installed locally, we can install the RStudio IDE. We'll install the free version of RStudio Desktop. For all available versions, see RStudio downloads.

  1. Import the code signing key for RStudio.

    sudo gpg --keyserver keyserver.ubuntu.com  --recv-keys 3F32EE77E331692F
    
  2. Download the Debian Linux Package file (.deb) for R Studio for Ubuntu. File will be in the format rstudio-{version}-amd64.deb. For example:

    export rstudiover="1.4.1717"
    wget --quiet -O rstudio.deb https://download1.rstudio.org/desktop/bionic/amd64/rstudio-$rstudiover-amd64.deb
    
  3. Use gdebi to install RStudio. Make sure to use the file path to indicate to apt that were installing a local file.

    sudo apt install gdebi-core 
    echo "y" | gdebi rstudio.deb –quiet
    

CRAN packages

Now it’s time to install any CRAN packages you want. First, add the current R 4.0 or later ‘c2d4u’ repository.

sudo add-apt-repository ppa:c2d4u.team/c2d4u4.0+

Use the install.packages(“package name”) command in an R interactive session as shown in quick list of useful R packages article. Alternately, use Tools -> Install Packages menu item in RStudio.

If you need help with finding a package, see a list of packages by task or alphabetic list of packages.

Cost

Let’s cover an example cost estimate for this class. Suppose you have a class of 25 students. Each student has 20 hours of scheduled class time. Another 10 quota hours for homework or assignments outside of scheduled class time is given to each student. The virtual machine size we chose was Small GPU (Compute), which is 139 lab units.

25 students × (20 scheduled hours + 10 quota hours) × 139 Lab Units × 0.01 USD per hour = 1042.5 USD

Important

The cost estimate is for example purposes only. For current pricing information, see Azure Lab Services pricing.

Next steps

The template image can now be published to the lab. For more information, see Publish the template VM.

As you set up your lab, see the following articles: