Installation and setup

For the first lecture already you need to install or upgrade R, RStudio, the tidyverse package and use a github account . We will help you with setting up your machine on the first day of the course.

Install

R is available for free for Windows, GNU/Linux and macOS.
The version required for this course is 4.3.1.
As for now, the latest version is 4.3.2 but there is no need to update. Participants using earlier versions (in particular 4.1.2 and before) will not be able to complete all examples presented. Please update.

Windows

Visit the download page and choose the corresponding installer for your platform. Rtools of the same version as your R is required for some packages used in this course and therefore required.

macOS

Browse to the download page and take it from there.

GNU/Linux

Any GNU/Linux distributions is bundled with a great package manager such as dpkg or rpm. Visit the appropriate web-page for your distribution here.

After you get the correct entry in your package manager, it should be quite easy as:

sudo apt-get install r-base r-base-dev

Install RStudio IDE

RStudio Desktop is an Integrated Development Editor wrapping and interfacing R that needs to be installed first. The free-version contains everything you need.

As for now, the latest version is 2023.06.2.+561. We recommend updating if you’re using older versions.

Check RStudio

They are 4 main panels in RStudio, but as the top-left is for scripting and by default missing, the layout should looks like

Of note, the default theme is with a white background. You can change it in the Global options > Appearance > Editor theme. Here is displayed the Cobalt theme.

Install R packages from the CRAN

CRAN - the Comprehensive R Archive Network - is the general package repository for R.

The bottom-right panel holds five tabs.

  • Files
  • Plots
  • Packages
  • Help
  • Viewer

Click on the Packages tab (1.) and select the select the Install button (2.). Type dplyr in (3.). You will see the word auto-completion that helps.

It takes some time, usually 10 seconds up to several minutes for a single package depending on its size and compilation stage.

The packages can also be installed in the console (i.e. the bottom-left panel). The snapshot below shows you how to install remotes using the console:

Of note, remotes is handy when you want to install a development version, for example for a new feature or if a bug was fixed but the package not yet on CRAN.

remotes::install_github("r-lib/remotes")

Installing packages

In the course, we will learn how to manage package installations by automation but to get started it’s necessary to install a few packages manually.

Installing the tidyverse

Installing the tidyverse is straight-forward using the package manager in RStudio. You can also use the command line. Please check that you’re using version 2.0 or greater if you’re using an existing installation of R.

install.packages("tidyverse")

If you have several cores, you may speedup the install with multi-threading (here for 2 cores):

install.packages("tidyverse", Ncpus = 2L)

Dependent libraries

MacOS

If you need to compile some packages, and binaries are not available, you need to compile from source. Necessary tools like compilers could be missing, you can obtain them from Apple with this command in a Terminal (be aware, it is a big: 8-12 GB):

xcode-select --install

GNU/Linux

Several libraries are mandatory for successfully installing the tidyverse.

If you encountered issues, install them in a terminal (not the R console) with

sudo apt install -y libcurl4-openssl-dev libssl-dev libxml2-dev

Then install the tidyverse as above.

Other packages

Please install also rmarkdown, knitr and renv or upgrade them.

install.packages(c("rmarkdown", "knitr", "renv"))
update.packages(c("rmarkdown", "knitr", "renv", "tidyverse"))

We will explore better ways of dealing with packages through the package manager renv in this course.


Testing your installation

Copy / paste the code below and you should obtain the following plot.

It ensures that you are using R version >= 4.1, readr >= 2.0 and a recent RStudio.

library(tidyverse)
stopifnot(rstudioapi::isAvailable(version_needed = "2022.02.2.485"))
read_csv("https://biostat2.uni.lu/practicals/data/swiss.csv",
         show_col_types = FALSE) |>
  pivot_longer(cols = c(Fertility, Agriculture),
               names_to = "measurement", 
               values_to = "value") %>%
  ggplot(aes(x = value, y = Education, colour = measurement)) +
  geom_point() +
  geom_smooth(method = "loess", formula = "y ~ x") +
  theme_bw(14)

If not, please contact Roland Krause with the error produced.

Code versioning

Git installation

Git has become the prevalent code versioning system and will be used in this course.

Windows

The recommended way to integrate Git with RStudio on Windows is via Git Bash, which will install other useful tools. Please download it from Git download and start it.

macOS

Check if you have Git already installed by running in the terminal:

git version

This should put out the current version of your Git installation or an error message such as command not found.

If you do not have Git installed please run the following line in your terminal to install it:

xcode-select --install

GNU/Linux

Check if you have Git already installed by running in the terminal:

git version

This should put out the current version of your git installation or an error message such as command not found.

If you do not have Git installed please run in the terminal:

sudo apt-get install git

Git configuration

Windows/macOS/GNU/Linux users

In RStudio, git should work out of the box but the git tab can be missing in first installations. Create an RStudio project and go to the Terminal pane of RStudio, Git Bash or the terminal in macOS or Linux and perform the first steps as commands, such as git init and git add.

After you have installed Git/Git Bash, configure it by typing in the terminal/Bash:

git config --global user.name "Firstname Lastname"
git config --global user.email "yourEmail@server.com"

Check the configuration by running:

git config --list

You should be able to see your user.name and user.email set accordingly.

Configure your machine to access GitHub

This course is using GitHub classrooms. Please make sure you have an account that you can access.

Go the GitHub signup page. A free account will do for the exercises. We recommend setting up two-factor authentication right away.

If you use older accounts, make sure that you have a working system.

A SSH key deposited at GitHub and connections via ssh are the method of choice for this course.

How SSH (Secure SHell) works briefly is presented below:

SSH Figure from the UL-HPC technical documentation

Check if a SSH key already exists by running in the terminal/Bash:

ls -al ~/.ssh

If there are id_rsa and id_rsa.pub files you already have a pair of SSH keys (private and public, respectively).

If you do not have it yet you have to generate it with the following lines:

  • Ensure that the folder .ssh is present:
mkdir -p ~/.ssh
  • Generate the key pair:
ssh-keygen -t rsa -n 4096

Accept the default location by pressing ENTER to the question Enter file in which to save the key (...)

It will then ask you to generate a password, it is recommended to have one.

The successful output should be something like:

[...]
Your identification has been saved in /your_home/.ssh/id_rsa
Your public key has been saved in /your_home/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:snJvBSQLR3SpqqCzSRM6g7Alw88f98cVvbgvnYxawAw root@6ca9e086bf35
The key's randomart image is:
+---[RSA 3072]----+
|     oo ..       |
|    . o.o        |
|     o = E    .  |
|.     o . +  . . |
|+o.  .. S. +  o .|
|==+ .  o  . .o . |
|B+ +..o. .. ..= .|
|++o .oo.o  o.+ + |
|oo   . ...... o. |
+----[SHA256]-----+

If you have entered a password, you need to add the private SSH key (id_rsa) to the ssh-agent by running the following commands:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

First command returns the process ID of the agent (for example Agent PID 247), the second should return Identity added: /your_name/.ssh/id_rsa

To use the SSH key you should copy the public SSH key (id_rsa.pub) to the clipboard. You can display it with:

cat ~/.ssh/id_rsa.pub

Of note, you can use nano or any text editor to open the id_rsa.pub file.

The key starts with ssh-rsa and ends with your email, like this example:

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDZfJj4aQ8gNz4XEfK6LadlKyQ7bhceHNb8PWrzb3Xo116ENFEDF29+1YB60cJItDqbEYmNONRX08N0f0EuOsRCwpiBXhWAb1bzB50hvmMB6FV9vctoLFjwrqaSBrFxQGVOguv6HGUAKt9G8Tx6H/5kwq1Ek7w2y8lxWeCWhiUUqhTC1UZ0k8FsoPyghsJueXJ0S1oRLYNADh59JJVniSLBN+bNcqk8DHyl0kOfn3WeIGNWVEt3Qqb2zan+DTbqOQacsphr7o6htaV2axe3dkR9ff4NmHQUPEL+9kFAt4UOnKc8OHil8vpYwh03UlN86fjVlEAypEgUo/0OZdkeRMbjR9bZ46mcKc6dSSI53W03hP6unldYp1qwHYHMTkuseiqKfY8BVtO6QLE0CYpqs5YPi2Pmkq9ghaEpP+UgsZxZ+a0tTZ9Ckv3kBBoBzGr0NCXQmAA2UmHrbWvEq0vfDvSnP20zJERWxrg6sf7GXEJRmPFkpxPvjDYrmJjFPfBvT0= name@domain.com

Select and copy the totality to the clipboard.

Finalise the connection

  1. Go to your GitHub account, open Settings

  1. In the SSH and GPG keys section add your public SSH key by clicking the New SSH key button.

  1. Give the name of your choice to the key (my_key here) and paste it in the Key field.

  1. Check that the SSH key is validated, as below

More information can be found on this page.