Chapter 1 Getting Started with R and RStudio

1.1 A Beginner’s Guide for First-Year Graduate Students

1.2 What You’re Installing and Why

Before we start, here’s what each tool does:

  • R: The programming language that does the actual data analysis
  • RStudio: A user-friendly interface that makes R easier to use (like Microsoft Word for documents)
  • Git: Software that helps you download and sync course materials
  • Additional tools: Help R work with different types of data and create documents

Important: Install everything in the order listed below. Each step builds on the previous one.

1.3 Step 1: Install R

R is the programming language. Install this first!

1.3.1 For Windows:

  1. Go to R Project website
  2. Click “base” then “Download R for Windows”
  3. Run the downloaded file and follow the installation prompts

1.3.2 For macOS:

These instructions work for ALL Macs (Intel and Apple Silicon):

  1. Go to R Project website
  2. Download the Intel (x86_64) version (yes, even for newer Macs with M1/M2/M3 chips)
  3. Run the downloaded .pkg file and follow the installation prompts

1.4 Step 2: Install Additional Tools (macOS Only)

Windows users can skip to Step 3. Mac users need these tools for R to work properly:

1.4.1 Install Required System Tools:

Open Terminal (found in Applications → Utilities) and paste each command one at a time:

xcode-select --install

Click “Install” when prompted and wait for it to finish (this may take 10-15 minutes).

1.4.2 Install Graphics Support:

  1. Go to XQuartz.org
  2. Download and install XQuartz
  3. Log out and log back in to your Mac after installation

1.4.3 Install Fortran Compiler:

  1. Go to R Tools for macOS
  2. Download gfortran-12.2-universal.pkg
  3. Install it by double-clicking the downloaded file

1.4.4 Add Fortran to Your System Path:

In Terminal, paste these commands:

echo 'export PATH="/opt/gfortran/bin:/usr/local/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/gfortran/bin:/usr/local/bin:$PATH"' >> ~/.bash_profile

Quit terminal and restart. In terminal, type:

gfortran --version

It should say GNU Fortran (GCC) 12.2.0. You’re all set.

1.5 Step 3: Install Git

Git helps you download and sync course materials.

1.5.1 For Windows:

  1. Go to git-scm.com
  2. Download Git for Windows
  3. Run the installer using all default settings (just keep clicking “Next”)

1.5.2 For macOS:

If you completed Step 2, Git is already installed! To verify, open Terminal and type:

git --version

You should see version information.

1.5.3 Configure Git (All Users):

Open Terminal (Mac) or Git Bash (Windows) and run these commands with your information:

git config --global user.name "Your Full Name"
git config --global user.email "your.email@university.edu"

1.6 Step 4: Install RStudio

RStudio is the user-friendly interface for R.

  1. Go to RStudio.com
  2. Download RStudio Desktop (the free version)
  3. Install it like any other program

1.6.1 For Mac Users with Apple Silicon (M1/M2/M3):

After installing RStudio:

  1. In Finder, go to Applications
  2. Right-click on RStudio.app
  3. Select Get Info
  4. Check the box “Open using Rosetta”

1.7 Step 5: Test Your Installation

Let’s make sure everything works:

1.7.1 Test RStudio and R:

  1. Open RStudio (not R - always use RStudio)
  2. You should see 4 panels:
    • Console (bottom left) - this is where you type R commands
    • Environment (top right) - shows your data
    • Files/Plots (bottom right) - shows files and graphs
    • Script (top left) - for writing longer code
  3. Click in the Console (the bottom left panel where you see >)
  4. Type this and press Enter:
2 + 2

You should see [1] 4

1.7.2 Test Git Connection:

  1. In RStudio, go to File → New Project → Version Control
  2. You should see “Git” as an option
  3. If you see it, great! If not, restart RStudio and try again

1.8 Step 6: Download Course Materials

Now let’s get the main course files:

  1. In RStudio, go to File → New Project → Version Control → Git
  2. In the “Repository URL” field, paste:
https://github.com/gurinina/2025_IntroR_and_RStudio
  1. Choose where to save the project on your computer
  2. Click “Create Project”

RStudio will download all course materials. This may take a few minutes.

1.8.1 Additional Repositories:

As the course progresses, you may need to clone additional repositories for specific modules. Your instructor will provide those URLs when needed.

Tips: - To update materials later in the term, open the project and click the Pull button in the Git tab - To switch between projects later: RStudio → File → Open Project… and select the corresponding .Rproj file

1.8.2 Understanding the Git Pane:

When you open your course project in RStudio, you should see a tab labeled Git in the upper-right panel (next to Environment and History). This is where you can:

  • See changes you’ve made to files (they’ll appear in the list)
  • Revert changes if you made an edit you don’t want to keep
  • Pull to update your local copy with the newest course materials from GitHub
  • Push to send your own changes to GitHub (not applicable in this course — you won’t be pushing)

For this course, you will mainly use the Pull button (blue down-arrow icon) to update your files when the instructor posts new material.

1.8.3 IMPORTANT: Git Best Practices for Students

To avoid problems when updating course materials, follow these rules:

1.8.3.1 ✅ Safe Things to Do:

  • Create new files for your notes and practice work
  • Rename template files before editing them (like codebook_yourname.Rmd)
  • Make new folders like my_notes/ or homework/ for your work
  • Copy lesson files to a new name before modifying them

1.8.3.2 ⛔ Things That Cause Git Conflicts:

  • Don’t edit the original lesson .Rmd files directly
  • Don’t modify existing file names in the lessons folder
  • Don’t save your work in the main lesson directories

1.8.3.3 Golden Rule:

Never edit the original lesson files directly. Always create new files for your notes and practice. This way you can always Pull the latest updates without problems!

For the codebook: Copy 17-introR_codebook_template.Rmd and rename it to <lastname>_17-introR_codebook.Rmd (replace “lastname” with your actual last name). This file includes homework questions and space for your notes.

1.8.3.4 Suggested Workflow:

2025_IntroR_and_RStudio/
├── lessons/               # DON'T TOUCH - instructor files  
├── img/                   # DON'T TOUCH - course images
├── _book/                 # DON'T TOUCH - bookdown output
├── 17-introR_codebook_template.Rmd # DON'T EDIT - copy/rename instead
├── (other course files)   # DON'T TOUCH - various course materials
├── student_notes/             # YOUR FOLDER for notes
│   ├── <lastname>_17-introR_codebook.Rmd
│   ├── practice.R
│   └── other_notes.Rmd

1.9 Step 7: Install Required R Packages

R packages are like apps that add extra features. We need several for this course.

1.9.1 Important: Locate the Console First

Before running any code, you need to find the Console in RStudio:

  1. Open RStudio
  2. Look at the bottom left panel - this is the Console
  3. You’ll see a > symbol - this is where you type R commands
  4. Click in this area to make sure your cursor is active in the Console

1.9.2 COPY AND PASTE THIS CODE INTO THE CONSOLE:

Click in the Console (bottom left panel with the > symbol) and paste this entire block of code:

# Set CRAN mirror first (this tells R where to download packages)
options(repos = c(CRAN = "https://cloud.r-project.org/"))

# Install basic tools first
install.packages(c("BiocManager", "devtools", "ggraph", "tidyverse"))

# Install Bioconductor (for biological data analysis)
if (!requireNamespace("BiocManager", quietly = TRUE)) {
    install.packages("BiocManager")
}

# Install all required packages for the course
BiocManager::install(c(
"apeglm", "bookdown", "clusterProfiler", "colorspace", "DESeq2", "details", "dplyr", "enrichplot", "fansi", "fgsea", "ggplot2", "ggrepel", "ggridges", "ggthemes", "gplots","GO.db", "GOSemSim", "igraph", "knitr", "org.Hs.eg.db", "patchwork", "pheatmap", "purrr", "RColorBrewer", "rmarkdown", "rsconnect", "scales",  "tidydr", "tidyr" ,"tidyverse", "tinytex",  "visNetwork"
))

# Install course-specific package
devtools::install_github("gurinina/GOenrichment", force = TRUE)

# Install document creation tools
if (!tinytex::is_tinytex()) {
    tinytex::install_tinytex(force = TRUE)
}

1.9.3 After Pasting the Code:

  1. Press Enter to run all the commands
  2. Be patient! This process takes 10-15 minutes and downloads many packages
  3. Don’t worry about the scrolling text - this is normal
  4. If prompted to install from source, type n and press Enter
  5. If asked to restart R, type y and press Enter

1.10 Step 8: Verify Everything Works

Let’s test that all packages installed correctly.

1.10.1 COPY AND PASTE THIS TEST CODE INTO THE CONSOLE:

Again, click in the Console (bottom left panel) and paste this code:

# Check if key packages work
library(ggplot2)
library(dplyr)
library(DESeq2)

# If no error messages appear, you're ready!
cat("Success! All packages are working.\n")

If you see “Success! All packages are working.” then everything is installed correctly!

1.11 What Success Looks Like

✅ RStudio opens without errors
✅ You can type 2 + 2 in the Console and get [1] 4
✅ You have a folder with course materials from the main repository
✅ The test code runs without errors and shows “Success!”

1.12 Common Problems and Solutions

1.12.1 “Package not found” errors:

  • Make sure you’re typing in the Console (bottom left panel)
  • Try restarting RStudio and running the code again
  • Make sure you pasted the CRAN mirror line: options(repos = c(CRAN = "https://cloud.r-project.org/"))

1.12.2 “Command not found” in Terminal:

  • Close and reopen Terminal
  • Try restarting your computer

1.12.3 RStudio can’t find Git:

  • Make sure you installed Git before RStudio
  • Restart RStudio completely (quit and reopen)

1.12.4 Installation seems stuck:

  • Be patient! Package installation can take 15-30 minutes
  • As long as you see text appearing, it’s working

1.12.5 “trying to use CRAN without setting a mirror” error:

  • Make sure you ran the first line: options(repos = c(CRAN = "https://cloud.r-project.org/"))
  • Restart RStudio and try again

1.13 Getting Help

If something doesn’t work:

  1. Try restarting RStudio
  2. Try restarting your computer
  3. Ask a classmate or instructor
  4. Email the instructor with:
    • What step you’re on
    • What error message you see (copy and paste it)
    • A screenshot if helpful

Remember: Software installation can be tricky, even for experienced users. Don’t worry if you need help - this is completely normal!

1.14 Next Steps

Once everything is installed:

  1. Explore RStudio - click around and see what’s in each panel
  2. Try the built-in R tutorial: In the Console, type:
install.packages("swirl")
library(swirl)
swirl()
  1. Read the course materials you downloaded
  2. Don’t panic! Learning R takes time, and everyone starts as a beginner

Welcome to R! You’re ready to start your data analysis