Motivation

  • Be able to use R on ubuntu 24.04
  • Easy to have different versions of R with different packages
  • Faster installation of pacakges (the downloaded packages are already compiled) so it si supposed to be much less annoying waiting for the compilation as in desktop Rstudio instance

Using bioconductor dockers

link to github of bioc-script which should be better to run specfic versions

link to the bioconductor docker website

How to do it with a normal R installation

1. Download and install R from the repository

# update indices
sudo apt update -qq
# install two helper packages we need
sudo apt install --no-install-recommends software-properties-common dirmngr
# add the signing key (by Michael Rutter) for these repos
# To verify key, run gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc 
# Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# add the repo from CRAN -- lsb_release adjusts to 'noble' or 'jammy' or ... as needed
sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"
# install R itself
sudo apt install --no-install-recommends r-base

NOTE: When trying to install tidyverse package run up into errors, chatgpt suggested installing following packages:

sudo apt update

sudo apt install \
  libcurl4-openssl-dev \
  libfontconfig1-dev \
  libfreetype6-dev \
  libxml2-dev \
  libharfbuzz-dev \
  libfribidi-dev \
  pkg-config \
  build-essential

and then some more

sudo apt update

sudo apt install \
  libcurl4-openssl-dev \
  libfontconfig1-dev \
  libfreetype6-dev \
  libpng-dev \
  libtiff5-dev \
  libjpeg-dev \
  libwebp-dev \
  libharfbuzz-dev \
  libfribidi-dev \
  libxml2-dev \
  pkg-config \
  build-essential

2. Install R studio

  1. download the deb from the posit website link
sudo apt install ./deb-name.deb

3. Run R script from the command line

Rscript name-of-script.R

After installing the tidyverse inside the Rstudio the scripts can be run from the command line.

Using docker to run R studio

Resources

  1. Website for docker link Notes: They have some AI trained on the docs, looks a bit overwhelming, did not finda a downloadable pdf file
  2. Website for rocker project link Notes:
  • describe the different projects quite understandably, rstudio/tidyverse/verse/geospatioal,
  • Additional images have the r2u project which looks nice (binary packages from CRAN, fast to install), did not check it yet

Make a folder for the R-projects in the /home directory

mkdir ~/R-projects
sudo chown -R $USER:$USER ~/R-projects

This makes a project for all R-projects which will be used to store data.
You must make the folder accessible to the user otherwise you are not able to access it.

Start the container

docker run -d --name rstudio44 \
  -p 8787:8787 \
  -e PASSWORD=vld \
  -e USERID=$(id -u) \
  -e GROUPID=$(id -g) \
  -v ~/R-projects:/home/rstudio/ \
  -v ~/Rlibs/4.4:/usr/local/lib/R/site-library \
  rocker/rstudio:4.4.1

What is what:

  • -p 8787:8787 the 8787 port of container (second number) maps to localhost:8787 (first number) in case you have more different rstudios, keep changing the first 4 digits before colon
  • -v ~/R-projects:/home/rstudio/R-projects tells the docker instance that the project is located in ~/R-projects
  • -v ~/Rlibs/4.4:/usr/local/lib/R/site-library tells it the packages are stored outside of the container and does not get wiped out after it stops
  • rocker/rstudio:4.4.1 the version of r studio you want to run

FAT NOTE: It is probably much better to use the docker compose compose.yml where this stuff is written out. this command line setting brings along the mess of finding all the config only in the JSON file, which is pretty difficult to navigate

Working with docker

Check the whole JSON with the settings

docker inspect rstudio44 | less

It is very long and not easy to go through, so you can check mounts etc using some targeted syntax

so use grep

docker inspect rstudio44 | grep -i -C 5 mount

Check the mounts

docker inspect -f '{{range .Mounts}}{{println .Source "->" .Destination}}{{end}}' rstudio44

Add another folder to the directory in docker

Example (add ~/data into /home/rstudio/data):

docker run -d --name rstudio44 ... -v "$HOME/data:/home/rstudio/data" rocker/rstudio:4.4.1

Log in

Open your browser and write http://localhost:8787 this will open a widget to put your name “rstudio” and “password”

Stop it

in the terminal run

docker stop rstudio44

Start it

docker start rstudio44

Remove it

docker rm rstudio44

FAT NOTE:

  • Be very careful to check that all the data you want to keep are in the bind volume, otherwise you lose it irrevrsibly (nothing is irreversible, but is gonna be a pain to get it back)
  • Making the volume at the root of the Rstudio as described above should make sure that whatefver is in “Home” in your Rstudio is safe, but double check before running the docker rm rstudio44 or similar
  • In case you are playing with the “upgrading” better to make a new one with a new name and then only delete the old one

More versions of R?

Give different versions of R different names, ports and Rlibs folders, and use a different rstudio versions then make a shortcut