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
R is the programming language we’ll use. Download it from:
RStudio is the interface that makes R easier to use. Download it from:
https://posit.co/download/rstudio-desktop/
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 packageOpen 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 │
│ │ │
└─────────────────────────────────────┴─────────────────────────────────────┘
Go to File → New File → R Script
In the new script (top left), type:
Place your cursor on the first line
Press Ctrl + Enter (or Cmd + Enter on Mac) to run each line
Watch the Console show the results!
Go to File → Save and save your script as “practice.R”
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!
In RStudio, go to File → New Project…
Choose “New Directory”
Choose “New Project”
Fill in:
R_Ecology_WorkshopClick “Create Project”
What just happened?
R_Ecology_WorkshopR_Ecology_Workshop.RprojNow 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!
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!
setwd()| Path Type | Example | Portable? |
|---|---|---|
| Absolute (bad) | "C:/Users/John/Desktop/thesis/data/beetles.csv" |
❌ No |
| Relative (good) | "data/raw/beetles.csv" |
✅ Yes |
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.
Let’s create the project you’ll use during the workshop:
Create the project (if you haven’t already):
Insect_Ecology_WorkshopCreate the folder structure:
Download workshop data (we’ll provide links) and
put them in data/raw/
Create your first script:
scripts/01_data_exploration.RVerify your working directory:
Complete these exercises to familiarize yourself with R syntax.
# 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_summerYour turn: Create objects for: -
n_species = 45 - habitat_type = “grassland” -
is_protected = FALSE
# 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 30Your 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
# 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
Please confirm you have completed:
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
If you completed everything above, you’re ready!
During the workshop, we’ll:
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.