4  Installing R

Note: Before installing R, make sure that you have the latest gdal, geos libraries installed so terra, sf and other R packages compile correctly.

4.1 Installing the latest R version

These instructions are from how to set up R on Ubuntu.

First, open sources.list:

sudo nano /etc/apt/sources.list

Then add this:

deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo apt install libcurl4-openssl-dev libssl-dev libxml2-dev libfontconfig1-dev libcairo2-dev

Finally, install R:

sudo apt update
sudo apt install r-base

4.2 Installing fonts

https://stackoverflow.com/questions/61204259/how-can-i-resolve-the-no-font-name-issue-when-importing-fonts-into-r-using-ext

4.3 Installing R binaries for Ubuntu

4.3.1 RSPM

First, install rspm:

remotes::install_github("Enchufa2/rspm")

Then, edit .Rprofile:

usethis::edit_r_profile()

And add this:

rspm::enable() # wrap it in suppressMessages() to avoid the initial message

4.3.2 BSPM

bspm::enable()

install.packages("tidyverse")

4.4 Using rig

rig a tool to manage different version of R installation on your computer.

To install it (note that the destination path is not the same as the one shown on the rig website because R was installed in /usr/ and not /usr/local/):

4.4.1 On Linux Mint

curl -Ls curl -Ls https://github.com/r-lib/rig/releases/download/v0.5.0/rig-linux-0.5.0.tar.gz | sudo tar xz -C /usr/

4.4.2 On Ubuntu

curl -Ls curl -Ls https://github.com/r-lib/rig/releases/download/v0.5.0/rig-linux-0.5.0.tar.gz | sudo tar xz -C /usr/local

Once installed, one can install different versions of R like so:

rig add 4.1.0
rig add 4.2.0

Starting RStudio with a specific version of R can be done with:

rig rstudio 4.1.0

It is also possible to start RStudio with a specific version of R that is defined in the renv lcok file:

rig rstudio <renv-lockfile> 

4.5 RStudio

4.5.1 Installing themes

devtools::install_github("gadenbuie/rsthemes")

These can be visualized with:

rsthemes::try_rsthemes()

4.6 Compiling R with Intel MKL math libraries

4.6.1 Install Intel MKL

cd /tmp
wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB

sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB
sudo sh -c 'echo deb https://apt.repos.intel.com/mkl all main > /etc/apt/sources.list.d/intel-mkl.list'

sudo apt install intel-mkl-64bit-2020.1-102 

source /opt/intel/mkl/bin/mklvars.sh intel64

4.6.2 Compile R with MKL

Download and decompress R source files.

tar -zxvf R-4.0.0.tar.gz
./configure --enable-R-shlib --enable-threads=posix --with-x=yes --with-cairo --with-libpng --with-libtiff --with-jpeglib --with-lapack --with-blas="-fopenmp -m64 -I/opt/intel/compilers_and_libraries_2020.4.304/linux/mkl/include -L/opt/intel/compilers_and_libraries_2020.4.304/linux/mkl/lib/intel64 -lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -lpthread -lm"

./configure --enable-R-shlib --enable-threads=posix --with-x=yes --with-cairo --with-libpng --with-libtiff --with-jpeglib --with-lapack --with-blas="-fopenmp -m64 -I$MKLROOT/include -L$MKLROOT/lib/intel64 -lmkl_gf_lp64 -lmkl_gnu_thread -lmkl_core -lpthread -lm" --libdir=/usr/bin

make -j24
make pdf
make info
sudo make install
sudo make install-info
sudo make install-pdf

4.7 Minimal system dependencies for R packages installation on Ubuntu

  • https://datawookie.dev/blog/2022/08/linux-packages-for-r/
sudo apt install -y \
  libcurl4-openssl-dev \
  libssl-dev \
  libxml2-dev \
  libfontconfig1-dev \
  libharfbuzz-dev \
  libfribidi-dev \
  libfreetype6-dev \
  libpng-dev \
  libtiff5-dev \
  libjpeg-dev \
  libpq-dev

4.8 .Rprofile

Basic .Rprofilefile.

# rspm::enable()

if (interactive() && Sys.getenv("RSTUDIO") == "") {
  Sys.setenv(TERM_PROGRAM = "vscode")
  if ("httpgd" %in% .packages(all.available = TRUE)) {
    options(vsc.plot = FALSE)
    options(device = function(...) {
      httpgd::hgd(silent = TRUE)
      .vsc.browser(httpgd::hgd_url(history = FALSE), viewer = FALSE)
    })
  }
}

options(pillar.sigfig = 5)

options(repos="https://packagemanager.rstudio.com/cran/__linux__/jammy/latest")

# This is important if you are using RSPM on Linux outside RStudio
options(HTTPUserAgent = sprintf("R/%s R (%s)", getRversion(), paste(getRversion(), R.version["platform"], R.version["arch"], R.version["os"])))

# Use an external browser for displaying html files, such as {gt} and {xaringan}
options(vsc.viewer = FALSE)

# External browser for web apps, such as {shiny}
options(vsc.browser = FALSE)

# View help page in an external browser
options(vsc.helpPanel = FALSE)

# Use original data viewer
# options(vsc.view = FALSE)

4.8.1 Ressources

4.9 Libraries locations

To avoid getting message that packages can not be updated because the directory is not writable, comment out R_LIBS_SITE="/usr/local/lib/R/site-library/:${R_LIBS_SITE}:/usr/lib/R/library" in:

sudo vim /etc/R/Renviron.site
sudo vim /etc/R/Renviron