Chapter 5 Installing RStudio and R

RStudio is a powerful IDE specifically designed for working with R. In this chapter, we’ll install both R (the language) and RStudio (the interface).

Important order:

You must install R first, then RStudio second. RStudio needs R to work!

Think of it like: - R = The engine - RStudio = The dashboard and steering wheel

5.1 Part 1: Installing R

5.1.1 Step 1.1: Download R

Go to the Comprehensive R Archive Network (CRAN):

https://cran.r-project.org/

5.1.1.1 For Windows Users

  1. Click “Download R for Windows”
  2. Click “base”
  3. Click “Download R-4.x.x for Windows” (version number may vary)

5.1.1.2 For macOS Users

  1. Click “Download R for macOS”
  2. Choose the appropriate version for your Mac:
    • Apple Silicon (M1/M2/M3): Download R-4.x.x-arm64.pkg
    • Intel Macs: Download R-4.x.x-x86_64.pkg

Not sure which Mac you have?

Click Apple menu () → About This Mac: - If you see “Chip: Apple M1” (or M2/M3) → Download arm64 version - If you see “Processor: Intel” → Download x86_64 version

5.1.1.3 For Linux Users

  1. Click “Download R for Linux”
  2. Choose your distribution (Ubuntu, Debian, Fedora, etc.)
  3. Follow the specific instructions for your distribution

For Ubuntu/Debian:

# Update package list
sudo apt update

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

5.1.2 Step 1.2: Install R

5.1.2.1 Windows Installation

  1. Locate the downloaded .exe file (e.g., R-4.4.0-win.exe)
  2. Double-click to launch the installer
  3. Language: Select “English” (or your preference)
  4. Click “Next” through the setup wizard
  5. Installation directory: Keep the default (C:\Program Files\R\R-4.x.x)
  6. Components: Select “Core Files” (default is fine)
  7. Startup options: Choose “No” for customized startup
  8. Start Menu folder: Keep default
  9. Click “Install”
  10. Wait for installation to complete (~2-3 minutes)
  11. Click “Finish”

32-bit vs 64-bit:

Modern computers are all 64-bit. If the installer asks, choose 64-bit only.

5.1.2.2 macOS Installation

  1. Locate the downloaded .pkg file
  2. Double-click to launch the installer
  3. Click “Continue” through the introduction screens
  4. License: Click “Continue” and then “Agree”
  5. Installation Type: Keep default location
  6. Click “Install”
  7. Enter your Mac password when prompted
  8. Wait for installation (~1-2 minutes)
  9. Click “Close”

macOS Security Warning:

If you see “R-4.x.x.pkg can’t be opened”, right-click the file → “Open” → Click “Open” again in the security dialog.

5.1.3 Step 1.3: Verify R Installation

Let’s make sure R installed correctly!

5.1.3.1 Windows

  1. Open Command Prompt: Press Windows+R, type cmd, press Enter
  2. Type: R --version
  3. You should see something like: R version 4.4.0 (2024-04-24)

5.1.3.2 macOS/Linux

  1. Open Terminal
  2. Type: R --version
  3. You should see: R version 4.4.0 (2024-04-24 ucrt)

If R is not recognized:

  • Windows: You may need to add R to your PATH. Restart your computer first, then try again.
  • macOS/Linux: R should be automatically available in Terminal. If not, try running /usr/local/bin/R --version

If still not working, try reinstalling R and make sure to check “Add R to PATH” during installation.


5.2 Part 2: Installing RStudio

Now that R is installed, let’s install RStudio!

5.2.1 Step 2.1: Download RStudio Desktop

Go to the Posit (formerly RStudio) download page:

https://posit.co/download/rstudio-desktop/

The website should auto-detect your operating system. Click the big blue button:

  • Windows: “Download RStudio for Windows”
  • macOS: “Download RStudio for macOS 11+”
  • Linux: “Download RStudio for Ubuntu 22/Debian 11”

Need a different version?

Scroll down to “All Installers and Tarballs” to find other OS versions.

5.2.2 Step 2.2: Install RStudio

5.2.2.1 Windows Installation

  1. Locate the downloaded .exe file (e.g., RStudio-2024.04.0-735.exe)
  2. Double-click to launch the installer
  3. Click “Next” on the welcome screen
  4. Installation folder: Keep default (C:\Program Files\RStudio)
  5. Start Menu folder: Keep default
  6. Click “Install”
  7. Wait for installation (~1-2 minutes)
  8. Click “Finish”

5.2.2.2 macOS Installation

  1. Locate the downloaded .dmg file
  2. Double-click to open it
  3. Drag the RStudio icon to the Applications folder
  4. Eject the disk image (right-click on desktop icon → Eject)
  5. Open RStudio from Applications or Spotlight (⌘+Space, type “RStudio”)

5.2.2.3 Linux Installation (Debian/Ubuntu)

# Download the .deb file from the RStudio website, then:
sudo dpkg -i ~/Downloads/rstudio-*-amd64.deb

# Fix any missing dependencies:
sudo apt-get install -f

5.2.3 Step 2.3: First Launch of RStudio

When you first open RStudio, you’ll see a four-panel interface:

RStudio’s Four Panels:

  1. Console (bottom-left): Where you run R commands interactively
  2. Source (top-left): Where you write and edit R scripts (may not appear until you open a file)
  3. Environment/History (top-right): Shows variables, data, and command history
  4. Files/Plots/Help (bottom-right): File browser, plots, packages, and help documentation

5.2.4 Step 2.4: Test RStudio

Let’s run your first R code!

  1. In the Console (bottom-left), click after the > prompt
  2. Type this code and press Enter:
print("Hello from RStudio!")

You should see the output: [1] "Hello from RStudio!"

  1. Try a simple calculation:
5 + 10

Output: [1] 15

  1. Create a variable:
x <- 25
x * 2

Output: [1] 50

Everything working?

If you see the outputs above, congratulations! R and RStudio are installed and working correctly. 🎉

If you see errors like “R not found” or RStudio won’t open, try: - Restarting your computer - Reinstalling R first, then RStudio - Checking that you installed both (not just one)

5.3 Step 3: Configure RStudio Settings

Let’s optimize RStudio for this course.

5.3.1 3.1 Open Global Options

Click: Tools → Global Options

5.3.3 3.3 Click “OK” to Save Settings

5.4 Step 4: Install Essential R Packages

R packages extend R’s functionality. Let’s install the packages you’ll need for this course.

5.4.1 4.1 Install Tidyverse

Tidyverse is a collection of R packages for data science. In the RStudio Console, run:

install.packages("tidyverse")

This will take 5-10 minutes to install. You’ll see lots of text scrolling by — that’s normal!

Mirror selection:

RStudio may ask you to choose a CRAN mirror. Select one close to you (e.g., “USA (TN)” for Tennessee) or “0-Cloud” for automatic selection.

5.4.2 4.2 Install Additional Course Packages

Run these commands one at a time in the Console:

# Statistical modeling packages
install.packages("car")        # Type III ANOVA
install.packages("emmeans")    # Post-hoc comparisons
install.packages("lme4")       # Mixed models

# Design of experiments
install.packages("FrF2")       # Factorial designs
install.packages("agricolae")  # Agricultural statistics

# Data manipulation and visualization (if not in tidyverse)
install.packages("dplyr")
install.packages("ggplot2")

# Reporting and documentation
install.packages("knitr")
install.packages("rmarkdown")
install.packages("quarto")     # We'll also install Quarto separately

Installation taking forever?

  • Use a wired internet connection if possible (faster than Wi-Fi)
  • Close other programs to free up memory
  • You can install packages one at a time if bulk installation fails
  • Some packages have many dependencies — this is normal!

5.4.3 4.3 Verify Package Installation

After installation, test that packages load correctly:

library(tidyverse)

You should see a message like:

── Attaching core tidyverse packages ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2

Common error:

Error in library(tidyverse) : there is no package called 'tidyverse'

Solution: The package didn’t install. Run install.packages("tidyverse") again.

5.5 Video Tutorial: Installing R and RStudio

Video not loading?

Watch on YouTube: How to Install R and RStudio

5.6 Summary Checklist

Before moving to the next chapter, make sure you have:

Congratulations! 🎉

You now have a fully functional R and RStudio environment ready for statistical analysis!


Next: Chapter 6: Installing and Configuring Quarto