Module 1: Intro to R & RStudio

Install R and RStudio, navigate the IDE, and run your first code.

Learning objectives — After this module you will be able to:

  • Explain the difference between R and RStudio
  • Navigate the four panes of the RStudio IDE
  • Create and save an R Script
  • Run code in the console and from a script
  • Install and load a package
  • Set up an RStudio Project for organised work

1.1 What is R?

R is a free, open-source programming language designed specifically for statistical computing and data visualisation. It was first released in 1995 and is now one of the most widely used tools in ecology, marine biology, epidemiology, and data science.

Key facts about R:

  • Completely free and runs on Windows, macOS, and Linux
  • Thousands of contributed packages extend its capabilities
  • Excellent graphics via base R and ggplot2
  • Strong support for reproducible research
  • Large, friendly community — Stack Overflow, Posit Community

1.2 What is RStudio?

RStudio (made by Posit) is an Integrated Development Environment (IDE) for R. Think of R as the engine and RStudio as the dashboard that makes driving comfortable. You write your code in RStudio, which then sends it to R to be executed.

Note

Always install R first, then RStudio. RStudio requires R to be installed — it does not include R itself.


1.3 Installation

Step 1 — Install R

  1. Go to https://cran.r-project.org
  2. Click your operating system (Windows / macOS / Linux)
  3. Download the latest release (≥ 4.3.0)
  4. Run the installer with default settings

Step 2 — Install RStudio

  1. Go to https://posit.co/download/rstudio-desktop/
  2. Download the free RStudio Desktop edition
  3. Run the installer with default settings

Step 3 — Verify installation

Open RStudio. You should see the R version printed in the Console pane, for example:

R version 4.6.0 (2026-04-24) -- "Because it was There"

1.4 The RStudio Interface

RStudio is divided into four panes:

┌──────────────────────────┬───────────────────────────┐
│                          │                           │
│   Source (Script editor) │   Environment / History   │
│                          │                           │
├──────────────────────────┼───────────────────────────┤
│                          │                           │
│   Console                │   Files / Plots / Help    │
│                          │                           │
└──────────────────────────┴───────────────────────────┘
Pane Purpose
Source Write and save scripts (.R files) here
Console Type or paste code to run it immediately
Environment See all objects (data, variables) currently in memory
Files / Plots / Help Browse files, view plots, read documentation
Tip

Keyboard shortcut: Press Ctrl+Enter (Windows/Linux) or Cmd+Enter (macOS) to run the current line or selection from the Source pane.


1.5 Your First Lines of R

Click in the Console and type each line, pressing Enter after each:

# This is a comment — R ignores anything after #
2 + 2

"Hello, whale researcher!"

sqrt(144)

R responds immediately. The [1] before each result just means “first element of the output vector” — don’t worry about that yet.


1.6 Creating an R Script

Rather than typing in the console (which is not saved), write your code in a script:

  1. File → New File → R Script (or Ctrl+Shift+N)
  2. Type your code in the editor that opens
  3. Save with Ctrl+S — give it a descriptive name like module1_practice.R
  4. Run a line with Ctrl+Enter, or run the whole script with Source button
# module1_practice.R
# My first R script

# A simple calculation
3.14159 * 6^2          # area of a circle with radius 6

# Assign a value to a variable
whale_count <- 47
whale_count

# Print a message
print("R is working!")
Note

R uses <- as the assignment operator. You can also use =, but <- is conventional in R code.


1.7 Installing and Loading Packages

Base R is powerful, but packages dramatically extend it. Packages are installed once and loaded each session.

# Install a package (do this once, in the console)
install.packages("tidyverse")

# Load a package (do this at the top of every script that uses it)
library(tidyverse)

Install the packages we will use throughout this tutorial:

install.packages(c("tidyverse", "lubridate", "ggplot2", "knitr", "rmarkdown"))

This may take a few minutes. You will see a lot of text — that is normal.


1.8 RStudio Projects

Projects keep your files, working directory, and history together — an essential habit for reproducible research.

Create a project for this tutorial:

  1. File → New Project → New Directory → New Project
  2. Name it whale_tutorial
  3. Choose a sensible location (e.g., ~/Projects/)
  4. Click Create Project

RStudio restarts and your working directory is now set to the project folder. All relative file paths (like read.csv("data/whale_surveys.csv")) will work from here.

Tip

Always start work by opening your .Rproj file. Never use setwd() in scripts — it breaks on other people’s computers.


1.9 Getting Help

In R

# Open help page for a function
?sqrt
help("mean")

# Search for a term across all help pages
??whale

# See function arguments
args(read.csv)

# Check package documentation
vignette("dplyr")

You can also type in the Help pane search box, or press F1 with your cursor on any function name.

Online

In the past, the best resources were forums (StackOverflow), websites and blogs. Now we can add AI sites to that list. There are a bunch of different options, but I’ve been using Claude and it’s pretty amazing for some things. But it’s not perfect, so always review code carefully and make sure you understand what it’s doing.