Chapter 1 Getting started with R and RStudio

1.1 Introduction

Welcome to this guide on getting started with R, RStudio, and the course material repository. This document will guide you through the installation of R, RStudio, syncing with the live repository, and installing the necessary R packages.

1.2 Step 1: Installing R

R is a powerful language for statistical computing and data analysis. To install R, follow the instructions for your operating system:

1.2.1 For Windows:

  1. Go to the R Project website (Windows).
  2. Click on the link for Windows and then click “base.”
  3. Download the installer and follow the on-screen instructions.

1.2.2 For macOS:

  1. Go to the R Project website (macOS).

  2. Click on the link for macOS and download the installer.

  3. Install the additional dependencies:

    • XCode Command Line Tools: Download from the Apple Developer website or run the following command in the terminal (use the search bar if you don’t know where the Terminal.app is) and run

      sudo xcode-select --install

      Instructions are also here: Apple Developer and here: RTools.

    • XQuartz: Required for some R packages. Install from XQuartz.

    • Fortran: Needed for package compilation. Install from the macOS R tools page.

1.3 Step 2: Installing RStudio

RStudio is an IDE that makes it easier to work with R. To install RStudio:

  1. Go to the RStudio website.
  2. Download the free version of RStudio Desktop for your operating system (Windows or macOS).
  3. Run the installer and follow the installation instructions.

1.4 Step 3: Syncing with the Course Repository

We will be using materials from a GitHub repository for this course. Instead of cloning the repository with Git, RStudio has built-in Git support that makes syncing with the live version easy.

1.4.1 Syncing the repository using RStudio:

  1. Open RStudio and select File -> New Project -> Version Control -> Git. Note: If you don’t have git installed on your device, you might have to first download and install it from github. Close the Rstudio before installing git and re-open it afterwards.
  2. In the “Repository URL” field, paste the following URL for the course material repository:
    https://github.com/gurinina/2024_IntroR_and_RStudio
  3. Choose a local folder on your computer to store the project files.
  4. Click “Create Project” to clone the repository.

After the repository is cloned, you can sync your local files with the GitHub repository by clicking the “Pull” button in the “Git” tab in RStudio. This ensures that you always have the latest version of the course materials.

1.5 Step 4: Installing Required R Packages

Once you have R, RStudio, and the course repository set up, you will need to install some R packages to work with the course material.

Copy and paste the following command into the RStudio console to install all the required packages:

# Install the 'GOenrichment' package from GitHub
if (!requireNamespace("devtools", quietly = TRUE)) {
    install.packages("devtools")
}

devtools::install_github("gurinina/GOenrichment", force = TRUE)

# Install Bioconductor packages using BiocManager
if (!requireNamespace("BiocManager", quietly = TRUE)) {
    install.packages("BiocManager")
}

# Install required Bioconductor and CRAN packages
BiocManager::install(c("bookdown", "clusterProfiler", "DESeq2", "dplyr", "enrichplot",
    "fgsea", "ggplot2", "ggrepel", "gplots", "knitr", "org.Hs.eg.db", "pheatmap",
    "purrr", "RColorBrewer", "rmarkdown", "rsconnect", "tidyverse", "tinytex"))

# Check if TinyTeX is installed
if (!tinytex::is_tinytex()) {
    message("TinyTeX is not installed. Installing TinyTeX now...")
    tinytex::install_tinytex(force = TRUE)
} else {
    message("TinyTeX is already installed. Skipping installation.")
}

These packages provide tools for data visualization, manipulation, compiling documents, and much more.

1.6 Step 5: Verifying Your Installation

To verify that everything is installed correctly, load the packages by copying and pasting this into your RStudio console:

library(BiocManager)
library(bookdown)
library(clusterProfiler)
library(DESeq2)
library(devtools)
library(dplyr)
library(enrichplot)
library(fgsea)
library(ggplot2)
library(ggrepel)
library(GOenrichment)
library(gplots)
library(knitr)
library(org.Hs.eg.db)
library(pheatmap)
library(purrr)
library(RColorBrewer)
library(rmarkdown)
library(rsconnect)
library(tidyverse)
library(tinytex)

If the libraries load without any error messages, everything is set up correctly.

1.7 Step 6: Compiling the Course Book

The course materials include a book compiled with bookdown. You can compile the book into HTML to read it locally.

1.7.1 Running R code; compiling the book

Optional

To render the book run the knit_to_gitbook.R file in the main project directory, 2024_IntroR_and_RStudio. You can run this by opening the file in RStudio and clicking the ‘source’ button at the top of the script window. This will generate an HTML version of the book, which you can view locally. If it doesn’t automatically open, you can find the file in the _book directory in the main folder of the repository. Click on the index.html file to view the book in your browser.

1.8 Step 7: Running R Code

Once everything is set up, you can start experimenting with R by typing commands into the RStudio console.

For example, to calculate the mean of a set of numbers, type:

mean(c(1, 2, 3, 4, 5))
## [1] 3

You should see the result 3.

1.9 Additional Resources

Here are some helpful resources for learning more about R, RStudio, and the topics covered in this course:

This guide should help you get started with R, RStudio, and the course material. If you have any questions, feel free to ask during class or email me: .

Some hands on exercises are provided below to help you get started with R and RStudio.

1.10 Learn R Basics

In this book we will be using theR programming language for all our analysis. You will be introduced to R in the first lab. However, if you have no basic programming skills and no knowledge of R, you might want to start learning R before the course starts. The best way to do this is to complete an R tutorial to familiarize yourself with the basics of programming and R syntax.

Swirl is a great way to learn R interactively. It is an R package that turns the R console into an interactive learning environment where you can work at your own pace and choose your own adventure. You can install swirl and run it in the following way:

Run the code below in your R console to install swirl and start the interactive tutorial, don’t set eval = TRUE, as it will not work in a bookdown document.

install.packages("swirl")
library(swirl)
swirl()

Swirl will provide prompts and ask you what you want to learn. Choose R Programming, then choose any of the first nine lessons.

    1. Basic Building Blocks
    1. Workspace and Files
    1. Sequences of Numbers
    1. Vectors
    1. Missing Values
    1. Subsetting Vectors
    1. Matrices and Data Frames
    1. Logic
    1. Functions

There are many open resources and reference guides for R that will be helpful to you. Several examples are listed here: