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/

Figure 1. Download R according to your operating system.

  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/

Figure 2. Download RStudio according to your operating system.

  1. Scroll down to list of operating systems and choose yours
  2. Click Download
  3. Run the installer with default settings

Step 3: Verify Installation

  1. Open RStudio (not R!)

Figure 3. Open RStudio (left), not R (right).

  1. You should see a window divided into panels

Figure 4. Overview RStudio.

  1. In the Console (bottom left), type: 2 + 2 and press ENTER

Figure 5. Overview CONSOLE panel.

  1. If you see [1] 4, congratulations - R is working!
# Try this in your console:
2 + 2

Part 2: Understanding the RStudio Interface (15 minutes)

Open RStudio and familiarize yourself with the four panels:

Figure 6. Overview the four panels.

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 3: 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…

Figure 7. Creating New Project.

  1. Choose “New Directory”

Figure 8. Creating New Directory.

  1. Choose “New Project”

Figure 8. Create New Project.

  1. Fill in:
    • Directory name: R_Ecology_Workshop
    • Create as subdirectory of: Choose your Documents folder or a USB drive
  2. Click “Create Project”

Figure 9. Create Directory name, subdirectory and 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 by making few folders, that you will need it later. Below is the Project Folder Structure.

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!

In the Files pane (bottom right), click “New Folder” and create these folders:

Figure 10. Create Folders.

# 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!

Figure 11. Open R Project file.

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.

Figure 12. Data folder structure consist of raw folder (for all raw data) and processed folder (for all processed data).

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 4: 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 script 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
  "ggplot2",        # Visualization
  "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(ggplot2)
library(patchwork)

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

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.