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 set of packages to install different subsets of R, the primary one being
While Code Ocean's environments are based on a recent version of Ubuntu (18.04 code name "Bionic Beaver" or just "Bionic" – you'll see why this is important in a minute), the version of R it includes, 3.4.4, is about two years old (!). Luckily, there's a way to get newer versions of R to install on Code Ocean:
In the environment editor, click the gear icon next to apt-get. This will open the apt-get configuration window.
In "additional sources", copy and paste
deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/(for R 3.6) or
deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/(for R 4.0). This tells apt-get to search for packages on the R project's own repository.
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 should end up with something like this:
After you save your changes and get back to the environment editor, the last and crucial step is to add
r-base as an apt-get dependency. (You'll notice the various R installer options will show up automatically, which you can immediately add packages to.)