3 R

R is one of the main programming languages used in Shen Lab. We use R for statistical analysis, data cleaning, visualization, reproducible reports, package development, and many omics-related workflows. This chapter explains how to install R, RStudio, and the basic tools needed for daily work.

3.1 What To Install

New members who will use R should install:

  • R;
  • RStudio Desktop;
  • RTools or command line developer tools, if package compilation is needed;
  • Git, so RStudio can work with GitHub repositories;
  • commonly used R packages for data analysis and reporting.

If you use a lab server or a shared computing environment, check with your mentor before installing or updating system-level software.

3.2 Installing R

R should be installed before RStudio. RStudio is an editor and integrated development environment; it does not replace R itself.

3.2.1 macOS

Recommended steps:

  1. Go to the official R project website: https://cran.r-project.org/.
  2. Choose the macOS download page.
  3. Download the latest stable R installer for your Mac.
  4. Open the installer and follow the default steps.
  5. After installation, open Terminal and check:
R --version

Apple Silicon and Intel Macs may have different installers. If you are unsure which one to choose, ask your mentor or the PI before installing.

3.2.2 Windows

Recommended steps:

  1. Go to https://cran.r-project.org/.
  2. Choose the Windows download page.
  3. Select “base” and download the latest R installer.
  4. Run the installer and use the default options unless you have a specific reason to change them.
  5. Open Command Prompt or PowerShell and check:
R --version

Windows users who need to compile packages from source may also need RTools. Install RTools only when needed, or when a package installation message asks for it.

3.2.3 Linux

On Linux, installation depends on the distribution and whether you are using a personal computer, lab workstation, or server.

For Ubuntu or Debian-based systems, R can usually be installed through the system package manager or from CRAN instructions:

sudo apt update
sudo apt install r-base

On shared servers, do not use sudo or change system R without permission. Ask the person responsible for the server if you need a specific R version or package environment.

3.3 Installing RStudio

RStudio Desktop is the recommended local IDE for most lab members using R.

Recommended steps:

  1. Go to the Posit download page: https://posit.co/download/rstudio-desktop/.
  2. Download RStudio Desktop for your operating system.
  3. Install it using the default instructions.
  4. Open RStudio and confirm that it detects your R installation.
  5. In the RStudio Console, check:
R.version.string

If RStudio cannot find R, restart your computer first. If the issue remains, check whether R was installed correctly.

3.4 First RStudio Setup

After installing RStudio, configure a few basic settings.

Recommended settings:

  • use UTF-8 encoding for source files;
  • use spaces instead of tabs;
  • show file extensions in your operating system;
  • avoid saving .RData automatically when closing a project;
  • use RStudio Projects for organized work;
  • connect RStudio with Git if you will use GitHub.

To avoid hidden workspace state, set RStudio not to automatically save and restore .RData unless a project has a specific reason to do so.

3.5 Installing R Packages

R packages can be installed from CRAN using:

install.packages("tidyverse")

Commonly used packages include:

  • tidyverse for data cleaning, transformation, and plotting;
  • data.table for fast table operations;
  • readxl and openxlsx for Excel files;
  • ggplot2 for visualization;
  • patchwork or cowplot for figure layout;
  • rmarkdown, knitr, and bookdown for reports and websites;
  • devtools, usethis, and roxygen2 for package development;
  • BiocManager for installing Bioconductor packages.

Bioconductor packages should usually be installed through BiocManager:

install.packages("BiocManager")
BiocManager::install("limma")

For project-specific analyses, avoid installing packages randomly until you understand the project environment. Some projects may use renv, containers, or server-managed libraries.

3.6 Using R Projects

For each research project, use an RStudio Project when possible. An RStudio Project helps keep file paths, working directories, Git settings, and project files organized.

Recommended project structure:

project-name/
  data/
  code/
  results/
  figures/
  docs/
  project-name.Rproj

Good practice:

  • do not use absolute paths that only work on your computer;
  • keep raw data separate from processed data;
  • write scripts that can be rerun from a clean session;
  • use clear file names and dates when appropriate;
  • document package versions if the analysis needs to be reproducible.

3.7 Working With Data

When using R for lab projects:

  • keep raw data unchanged;
  • write scripts for each data-processing step;
  • save intermediate results only when they are useful and documented;
  • avoid manual spreadsheet edits unless they are recorded clearly;
  • make figures from code whenever possible;
  • keep analysis outputs in the agreed project folder.

For omics data, pay attention to sample metadata, feature identifiers, missing values, batch information, normalization steps, and quality-control results.

3.8 Reproducibility

A useful R analysis should be understandable and rerunnable by another lab member.

Recommended habits:

  • start scripts with a short description of the goal;
  • load packages at the top of the script;
  • use relative paths inside a project;
  • set random seeds when using random procedures;
  • save important parameters in code, not only in memory;
  • record package versions for analyses used in papers or reports;
  • restart R and rerun the analysis before sharing final results.

For important projects, consider using renv to record package versions:

install.packages("renv")
renv::init()
renv::snapshot()

Use renv only after discussing with your mentor if the project already has an environment-management strategy.

3.9 Getting Help

If you encounter R or RStudio problems, first collect:

  • your operating system;
  • R version;
  • RStudio version;
  • the exact error message;
  • the code that produced the error;
  • what you already tried.

Then ask your mentor, a relevant lab member, or the PI. Clear error reports make it much easier for others to help.