Installing R in a non-R environment, or updating to the most recent version

How to add a recent R version to any environment

Code Ocean's Starter Environments use Ubuntu Linux as their operating system. Ubuntu's package manager, APT/apt-get (which can be operated using Code Ocean's user-friendly environment editor) provides as a set of packages to install different subsets of R, the primary one being r-base.

Since R is a fast-moving project, the latest stable version isn’t always available from Ubuntu’s repositories. We can add the external repository maintained by CRAN to the apt-get configuration.

  1. In "additional sources", copy and paste deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/(for Ubuntu 20.04, please check the Hint below for other Ubuntu versions). This tells apt-get to search for packages on the R project's own repository.

  2. In "GPG Keys", copy and paste E298A3A825C0D65DFD57CBB651716619E084DAB9. This is a piece of information provided by the R project to guarantee the authenticity of the repository.

You can add a different source depending on the Ubuntu version of your starter environment. More details can be found in this documentation from the R project.

You should end up with something like this (for an image based on Ubuntu 20.04)

After you save your changes and get back to the environment editor, the last and crucial step is to add r-base-core and r-base-dev as an apt-get dependency. (You'll notice the various R installer options will show up automatically, which you can immediately add packages to. The system also adds some dependencies to apt-get as well)