Welcome!

This document will help you prepare for our 2-day R workshop. Please complete all sections before arriving - this will ensure we can dive straight into the exciting parts during the workshop!

Time needed: Approximately 1-2 hours


Part 1: Install R and RStudio (30 minutes)

Step 1: Install R

R is the programming language we’ll use. Download it from:

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

  1. Click on your operating system (Windows, Mac, or Linux)
  2. For Windows: Click “base”, then “Download R-4.x.x for Windows”
  3. For Mac: Click the .pkg file appropriate for your Mac
  4. Run the installer with default settings

Step 2: Install RStudio

RStudio is the interface that makes R easier to use. Download it from:

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

  1. Scroll down to “RStudio Desktop FREE”
  2. Click Download
  3. Run the installer with default settings

Step 3: Verify Installation

  1. Open RStudio (not R!)
  2. You should see a window divided into panels
  3. In the Console (bottom left), type: 2 + 2 and press Enter
  4. If you see [1] 4, congratulations - R is working!
# Try this in your console:
2 + 2

Part 2: Install Required Packages (15 minutes)

Packages add extra functionality to R. We need several for this workshop.

Copy and paste or type (help you to memorize and get use in typing commands in R) this entire block into your Console and press Enter:

# Install all required packages for the workshop
# This may take 5-10 minutes - be patient!

install.packages(c(
  "tidyverse",      # Data wrangling and visualization
  "vegan",          # Community ecology analyses
  "ape",            # PCoA and phylogenetics
  "indicspecies",   # Indicator species analysis
  "patchwork"       # Combining plots
))

Verify packages installed correctly:

# Try loading each package - you should see NO errors
library(tidyverse)
library(vegan)
library(ape)
library(indicspecies)
library(patchwork)

# If you see "Error in library(xxx) : there is no package called 'xxx'"
# Run: install.packages("xxx") for that package

Part 3: Understanding the RStudio Interface (15 minutes)

Open RStudio and familiarize yourself with the four panels:

┌─────────────────────────────────────┬─────────────────────────────────────┐
│                                     │                                     │
│         SOURCE / SCRIPT             │           ENVIRONMENT               │
│         (Top Left)                  │           (Top Right)               │
│                                     │                                     │
│   • Where you write and save code   │   • Shows objects you've created    │
│   • Create new: File → New File     │   • Your data will appear here      │
│     → R Script                      │   • History tab: past commands      │
│   • SAVE YOUR WORK HERE!            │                                     │
│                                     │                                     │
├─────────────────────────────────────┼─────────────────────────────────────┤
│                                     │                                     │
│           CONSOLE                   │        FILES / PLOTS / HELP         │
│         (Bottom Left)               │         (Bottom Right)              │
│                                     │                                     │
│   • Where R "talks back"            │   • Files: Browse your folders      │
│   • Type commands directly here     │   • Plots: View your graphs         │
│   • See results and errors          │   • Help: Documentation             │
│   • NOT saved! Use for testing      │   • Packages: Manage packages       │
│                                     │                                     │
└─────────────────────────────────────┴─────────────────────────────────────┘

Try This Exercise:

  1. Go to File → New File → R Script

  2. In the new script (top left), type:

    # My first R script
    x <- 10
    y <- 5
    x + y
  3. Place your cursor on the first line

  4. Press Ctrl + Enter (or Cmd + Enter on Mac) to run each line

  5. Watch the Console show the results!

  6. Go to File → Save and save your script as “practice.R”


Part 4: R Projects - THE MOST IMPORTANT SKILL! (20 minutes)

Why Projects Matter

This is the #1 mistake beginners make: Working without a project, with files scattered everywhere, and scripts that only work on one computer.

Without a project: - Files are scattered across your computer - You use setwd() with paths like "C:/Users/John/Desktop/thesis/data/..." that break on other computers - You can’t find your files months later - Collaborators can’t run your code

With a project: - Everything is in ONE folder - File paths are simple and portable - Easy to share with collaborators - Easy to find months/years later - Professional and reproducible!

Creating Your First Project

Step-by-Step:

  1. In RStudio, go to File → New Project…

  2. Choose “New Directory”

  3. Choose “New Project”

  4. Fill in:

    • Directory name: R_Ecology_Workshop
    • Create as subdirectory of: Choose your Documents folder or a USB drive
    • ☑ Check “Create a git repository” (optional but recommended)
    • ☑ Check “Use renv with this project” (optional)
  5. Click “Create Project”

What just happened?

  • RStudio created a folder called R_Ecology_Workshop
  • Inside is a file called R_Ecology_Workshop.Rproj
  • Your working directory is now automatically set to this folder
  • All your work will be saved here!

Project Folder Structure

Now let’s organize your project properly. In the Files pane (bottom right), click “New Folder” and create these folders:

R_Ecology_Workshop/
│
├── R_Ecology_Workshop.Rproj    ← Double-click this to open your project!
│
├── data/                       ← Put your CSV files here
│   ├── raw/                    ← Original data (NEVER modify!)
│   └── processed/              ← Cleaned data you create
│
├── scripts/                    ← Your R scripts
│
├── output/                     ← Results, tables
│
└── figures/                    ← Saved plots

Create these folders now!

# Or run this code to create folders:
dir.create("data")
dir.create("data/raw")
dir.create("data/processed")
dir.create("scripts")
dir.create("output")
dir.create("figures")

The Golden Rules of Projects

Rule 1: ALWAYS Work in a Project

Before doing ANY work: 1. Open RStudio 2. Go to File → Open Project 3. Navigate to your .Rproj file 4. Double-click to open

Or: Just double-click the .Rproj file in your file explorer!

Rule 2: NEVER Use setwd()

# ❌ BAD - Never do this!
setwd("C:/Users/John/Desktop/thesis/chapter2/analysis/data")
data <- read.csv("beetles.csv")

# This WILL break on:
# - Someone else's computer
# - Your new computer
# - When you move the folder
# ✅ GOOD - Use relative paths from your project folder
data <- read.csv("data/raw/beetles.csv")

# This works because:
# - RStudio automatically sets your project folder as working directory
# - The path is relative to the project
# - It works on ANY computer with the same folder structure

Rule 3: Use Relative Paths

Path Type Example Portable?
Absolute (bad) "C:/Users/John/Desktop/thesis/data/beetles.csv" ❌ No
Relative (good) "data/raw/beetles.csv" ✅ Yes

Rule 4: Keep Raw Data Sacred

data/
├── raw/           ← NEVER modify these files!
│   └── beetles.csv    Original data from field
│
└── processed/     ← Save cleaned data here
    └── beetles_clean.csv    Your cleaned version

Why? If you make a mistake cleaning data, you can always start over from the raw file.

Practice: Setting Up the Workshop Project

Let’s create the project you’ll use during the workshop:

  1. Create the project (if you haven’t already):

    • File → New Project → New Directory → New Project
    • Name: Insect_Ecology_Workshop
    • Location: Your Documents folder
  2. Create the folder structure:

    dir.create("data")
    dir.create("data/raw")
    dir.create("data/processed")
    dir.create("scripts")
    dir.create("output")
    dir.create("figures")
  3. Download workshop data (we’ll provide links) and put them in data/raw/

  4. Create your first script:

    • File → New File → R Script
    • Save as scripts/01_data_exploration.R
  5. Verify your working directory:

    getwd()  # Should show your project folder path

Part 5: R Basics Self-Study (30 minutes)

Complete these exercises to familiarize yourself with R syntax.

Exercise 1: Objects and Assignment

# Create these objects:

# 1. A number called 'n_sites' with value 12
n_sites <- 12

# 2. A text string called 'study_area' with value "Tropical Forest"
study_area <- "Tropical Forest"

# 3. A logical value called 'is_summer' set to TRUE
is_summer <- TRUE

# 4. Print each object by typing its name
n_sites
study_area
is_summer

Your turn: Create objects for: - n_species = 45 - habitat_type = “grassland” - is_protected = FALSE

Exercise 2: Vectors

# Create a vector of abundances
abundances <- c(12, 45, 23, 8, 67, 34)

# Calculate statistics
sum(abundances)      # Total
mean(abundances)     # Average
max(abundances)      # Maximum
min(abundances)      # Minimum
length(abundances)   # How many elements

# Extract elements
abundances[1]        # First element
abundances[3]        # Third element
abundances[c(1,3,5)] # Elements 1, 3, and 5
abundances[abundances > 30]  # Elements greater than 30

Your turn: 1. Create a vector called species_counts with values: 5, 12, 8, 3, 15, 22, 9 2. Calculate the mean 3. Find how many values are greater than 10

Exercise 3: Data Frames

# Create a simple data frame
my_data <- data.frame(
  site = c("A", "B", "C", "D"),
  habitat = c("forest", "forest", "grassland", "grassland"),
  beetles = c(23, 18, 45, 38),
  spiders = c(12, 15, 8, 11)
)

# View it
my_data

# Extract columns
my_data$beetles
my_data$habitat

# Extract rows
my_data[1, ]        # First row
my_data[my_data$habitat == "forest", ]  # Only forest rows

# Calculate mean beetles
mean(my_data$beetles)

Your turn: 1. Extract only the grassland rows 2. Calculate the mean number of spiders 3. Find which site has the most beetles

Exercise 4: Getting Help

# These are essential skills!

# Get help for a function
?mean
?sum

# Search for help on a topic
??diversity

# See examples
example(mean)

Checklist Before Workshop

Please confirm you have completed:

Troubleshooting

Problem: “Package installation fails” - Try: install.packages("packagename", dependencies = TRUE) - Check your internet connection - Try a different CRAN mirror: chooseCRANmirror()

Problem: “I can’t find my project” - Look for the .Rproj file in the folder you created - Check your Documents folder - In RStudio: File → Recent Projects

Problem: “I get an error when running code” - Read the error message carefully - Check for typos (R is case-sensitive!) - Make sure you ran all previous lines first


See You at the Workshop!

If you completed everything above, you’re ready!

During the workshop, we’ll:

  • Day 1: Import data, wrangle it, explore patterns, choose focal taxa
  • Day 2: Visualize, calculate diversity, ordination (NMDS), statistical tests

Bring: - Your laptop with R and RStudio installed - The workshop project set up - Questions!


If you have problems with installation, please contact the instructor BEFORE the workshop day.