Installation of R, RStudio & git

  Having an up-to-date environment is the first good practice. Please install the latest versions of , RStudio, Pandoc, Quarto, and git.


Installation

 Windows

Select Download R for Windows | Base | Download R 4.3.1 for Windows and follow instructions.

Download the version for Windows 10/11 and follow instructions.

Rtools4.3 is strongly recommended to install some packages from sources. It contains different utilities, in particular the utility make. Download the 64-bit version for Windows and follow instructions.

Download the 64-bit version for Windows and follow instructions (select the default options).

The package rmarkdown requires the software pandoc to convert document from one format (e.g. .Rmd) to another (e.g. .html). Download the latest version of pandoc (file with the extension .msi) at: https://github.com/jgm/pandoc/releases/, and follow instructions.

Quarto is a multi-language, next-generation version of R Markdown developed by Posit (formely RStudio Inc.), and includes dozens of new features and capabilities. Download the latest version of Quarto (file with the extension .msi) at: https://quarto.org/docs/get-started/, and follow instructions.

  In addition to this software you need to install the package quarto:

# Install quarto package ----
install.packages("quarto")

If you want to convert documents from .Rmd to .pdf, you will need LaTeX. You can install a lightened distribution using the package tinytex. Open RStudio and run:

# Install tinytex package ----
install.packages("tinytex")

# Install LaTeX distribution ----
tinytex::install_tinytex()

  If you already have a LaTeX distribution, do not install tinytex.

Open RStudio and run:

# Get R version ----
R.version.string
## "R version 4.3.1 (2023-06-16)"

# Check if git is installed ----
Sys.which("git")
##                  git
## "C:/path/to/git.exe"

# Check if Rtools is installed ----
Sys.which("make")
##                  make 
## "C:/path/to/make.exe"

# Check if Pandoc is installed ----
Sys.which("pandoc")
##              pandoc 
## "C:/path/to/Pandoc"

# Check if Quarto is installed ----
Sys.which("Quarto")
##              Quarto 
## "C:/path/to/Quarto"

# Check if LaTeX is installed ----
Sys.which("pdflatex")
##              pdflatex 
## "C:/path/to/pdflatex" 

# Install package from sources ----
install.packages("jsonlite", type = "source")

  Go to the section Configuration to learn how to configure git & GitHub

 macOS

The easiest way to set up your environment is by using the package manager for macOS Homebrew. With this tool you can install (and update) your software directly from the terminal. First you need to install the Apple software Command Line Tools for Xcode.

Open a terminal, run this line and accept the license:

# Install Xcode Command Line Tools ----
sudo xcode-select --install

Then you can install Homebrew itself:

# Install Homebrew ----
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Check installation ----
brew --version
## Homebrew 4.1.11

# Update repositories (list of available software) ----
brew update

Follow these lines to install your environment:

# Install utilities for R ----
brew install --cask xquartz     ## Graphical window system
brew install gfortran           ## FORTRAN compiler for GCC

# Install R ----
brew install --cask r

# Install RStudio Desktop ----
brew install --cask rstudio

# Install git ----
brew install git

# Install pandoc ----
brew install pandoc             ## Convert documents (Rmd, html, etc.)

## Install Quarto ----
brew install --cask quarto      ## A next-generation of R Markdown

In addition you need to install the package quarto:

# Install quarto package ----
install.packages("quarto")

If you want to convert documents from .Rmd to .pdf, you will need LaTeX. You can install a lightened distribution using the package tinytex. Open RStudio and run:

# Install tinytex ----
install.packages("tinytex")

# Install LaTeX ----
tinytex::install_tinytex()

Important: If you already have a LaTeX distribution, do not install tinytex.

Open RStudio and check your installation:

# Get R version ----
R.version.string
## "R version 4.3.1 (2023-06-16)"

# Check if git is installed ----
Sys.which("git")
##            git
## "/usr/bin/git"

# Check if Rtools is installed ----
Sys.which("make")
##            make 
## "/usr/bin/make"

# Check if Pandoc is installed ----
Sys.which("pandoc")
##                  pandoc 
## "/usr/local/bin/pandoc"

# Check if Quarto is installed ----
Sys.which("Quarto")
##                  Quarto 
## "/usr/local/bin/Quarto"

# Check if LaTeX is installed ----
Sys.which("pdflatex")
##                  pdflatex 
## "/usr/local/bin/pdflatex" 

# Install package from sources ----
install.packages("jsonlite", type = "source")


  To keep your system up-to-date, run time to time:

# Update environment ----
brew update && brew upgrade --greedy


  Go to the section Configuration to learn how to configure git & GitHub


 Ubuntu

First check your version of Ubuntu in a terminal:

# Check Ubuntu version ----
cat /etc/issue
## Ubuntu 22.04 LTS


Table 1. List of CRAN repositories for different versions of Ubuntu
Release Release name Repository URL
22.04 LTS Jammy Jellyfish https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/
20.04 LTS Focal Fossa https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/
18.04 LTS Bionic Beaver https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/


Install by following these steps (change the URL below by the one specific to your version of Ubuntu, cf. Table 1) :

# Install APT utilities ----
sudo apt install build-essential software-properties-common wget

# Add CRAN GPG key ----
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 CRAN repository to APT repositories list ----
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/'

# Update packages list ----
sudo apt update

# Install R ----
sudo apt install r-base r-base-dev


To install RStudio Desktop visit this page: https://posit.co/download/rstudio-desktop/, download the .deb file for Ubuntu 22 (or Ubuntu 20/Debian 11 depending on your version) and install it.

Alternatively, you can install RStudio Desktop from the terminal:

# Download RStudio Desktop installer (Ubuntu = 22.04) ----
wget https://download1.rstudio.org/electron/jammy/amd64/rstudio-2023.06.2-561-amd64.deb

# Install RStudio Desktop ----
sudo dpkg -i rstudio-2023.06.2-561-amd64.deb

# Remove installer ----
rm rstudio-2023.06.2-561-amd64.deb

Now let’s install git:

sudo apt install git

And Pandoc (to convert documents with rmarkdown):

sudo apt install pandoc

Let’s install the Quarto software, a multi-language, next-generation version of R Markdown developed by Posit (formerly RStudio), that includes dozens of new features and capabilities.

# Download Quarto installer ----
wget https://github.com/quarto-dev/quarto-cli/releases/download/v1.4.371/quarto-1.4.371-linux-amd64.deb

# Install Quarto ----
sudo dpkg -i quarto-1.4.371-linux-amd64.deb

# Remove installer ----
rm quarto-1.4.371-linux-amd64.deb

In addition you need to install the package quarto:

# Install quarto package ----
install.packages("quarto")

If you want to convert documents from .Rmd to .pdf, you will need LaTeX. You can install a lightened distribution using the package tinytex. Open RStudio and run:

# Install tinytex ----
install.packages("tinytex")

# Install LaTeX ----
tinytex::install_tinytex()


Open RStudio and check your installation:

# Get R version ----
R.version.string
## "R version 4.3.1 (2023-06-16)"

# Check if git is installed ----
Sys.which("git")
##            git
## "/usr/bin/git"

# Check if Rtools is installed ----
Sys.which("make")
##            make 
## "/usr/bin/make"

# Check if Pandoc is installed ----
Sys.which("pandoc")
##            pandoc 
## "/usr/bin/pandoc"

# Check if Quarto is installed ----
Sys.which("Quarto")
##            Quarto 
## "/usr/bin/Quarto"

# Check if LaTeX is installed ----
Sys.which("pdflatex")
##            pdflatex 
## "/usr/bin/pdflatex" 

# Install package from sources ----
install.packages("jsonlite", type = "source")


  To keep your system up-to-date, run time to time:

## Update system ----
sudo apt update && sudo apt upgrade -y


  Go to the section Configuration to learn how to configure git & GitHub


Configuration

 git

To use git, you need to store your credentials (user name and email) that will be added to all your commits. Open a terminal (you can use the terminal of RStudio) and run:

git config --global user.name  "Jane Doe"
git config --global user.email "jane.doe@mail.com"

When you create a new git project the default branch will be named master. It’s recommended to replace master by main (for more information read this article).

On a terminal, run:

git config --global init.defaultBranch main

From now when you will initialize git in a new project the default branch will be main.


 GitHub SSH keys

To communicate with GitHub, you can use the protocol HTTPS (default) or the protocol SSH (recommended). The protocol SSH is a more secure method of logging into a remote server (GitHub). To use this protocol, you need to generate an SSH keys pair that consists in creating two long strings of characters: a public key and a private key. You can place the public key on any server (for instance GitHub), and then connect to the server using a device (your laptop) that has access to the private key.

To generate a new SSH keys pair proceed as follow:

  1. Create an account on GitHub at https://github.com/signup
  2. Open RStudio and click on: Tools > Global options > Git/SVN
  3. Click on Create RSA Key and then click on Create (you can omit the passphrase). This new SSH keys pair has been stored in ~/.ssh/.
  4. Click on View public key and copy it
  5. Go to the page https://github.com/settings/keys and click on New SSH key
  6. Choose a name for your key (e.g. your laptop name) and paste your public key.
  7. Finally click on Add SSH key


  If you are on Ubuntu you need to install ssh-askpass: sudo apt install ssh-askpass


Let’s test the SSH connection with GitHub. Open a terminal and run:

ssh -T git@github.com

When you first connect to GitHub (and to any remote server), you will be asked if you trust this server:

The authenticity of host '[92.168.45.3]:22' can't be established.
RSA key fingerprint is ...
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Just write yes and press Enter. This will add a line in the file ~/.ssh/known_hosts that lists all your trusted servers.

Congrats! You can now communicate with GitHub using the SSH protocol.


  From now, you need to use the SSH URL when connecting to a GitHub repo.

# SSH URL (use this one) ----
git@github.com:github_account/github_repo.git

# HTTPS URL ----
https://github.com/github_account/github_repo.git


  If you lose your private SSH key you won’t be able to establish a connection with GitHub. You’ll need to delete your SSH key on GitHub and to create a new one.


 GitHub PAT

If you want to use the GitHub API, essentially with the package usethis (and rcompendium), you need to create a GitHub Personal Access Token (PAT).

Proceed as follow:

  1. Go to https://github.com/settings/tokens
  2. Click on Generate new token (classic)
  3. Choose a name for your new token (e.g. your laptop name)
  4. Select no expiration date
  5. In the section Select scopes, select at least repo and workflow
  6. Copy this token.

Then open RStudio and run:

# Install usethis package ----
install.packages("usethis")

# Open ~/.Renviron file ----
usethis::edit_r_environ()
## • Modify '~/.Renviron'
## • Restart R for changes to take effect

In the ~/.Renviron file, add this new line: GITHUB_PAT='XXX' where XXX is your token. Do not forget to restart .


Finally let’s tell usethis to use the SSH protocol to communicate with GitHub. Open RStudio and run:

# Open ~/.Rprofile file ----
usethis::edit_r_profile()
## • Modify '~/.Rprofile'
## • Restart R for changes to take effect

In the ~/.Rprofile file, add this new line: options(usethis.protocol = "ssh"). Do not forget to restart .

Corrections

If you see mistakes or want to suggest changes, please Create an issue on the source repository.

Reuse

The material of this website is licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/frbcesab/training-courses/.