R Setup & Basics
This page is simply meant to help you get your R environment ready before we begin. It is not a comprehensive guide, just a practical checklist to make sure the basic tools are installed and working. We will use a small set of commonly used packages for data handling, visualisation, and multivariate methods. If you run through the installation and test steps below without errors, you should be in good shape for the course. If something does not work, do not worry. We can sort it out. The goal here is to reduce setup surprises later on.
Prerequisites
You should be comfortable with:
- Running R commands and using basic functions
- Installing and loading packages
- Working with vectors and data frames
Package Installation
install.packages(c(
"tidyverse", # Data manipulation and visualisation
"GGally", # Pairwise plots
"MASS", # Modern Applied Statistics with S
"cluster", # Cluster analysis
"vegan", # Ordination and PERMANOVA
"factoextra", # PCA visualisation
"FactoMineR", # PCA and correspondence analysis
"corrplot" # Correlation heatmaps
))
Attaching vs. calling packages
Packages need to be installed once but do not always need to be attached with library(). You can call individual functions directly using package::function() (e.g., vegan::adonis2()). This avoids namespace conflicts, a common issue when MASS or vegan are loaded alongside tidyverse, as several function names overlap.
Compilation requirements
Some packages may require compilation tools, particularly if a precompiled binary is not available:
- Windows: Rtools
- macOS: xcode-select --install
- Linux: sudo apt install r-base-dev
Test Your Setup
Run the following to confirm everything works before the course:
library(vegan)
library(ggplot2)
library(factoextra)
# PCA
data(iris)
pca <- prcomp(iris[, 1:4], scale. = TRUE)
fviz_pca_ind(pca, col.ind = iris$Species)
# Clustering
set.seed(123)
km <- kmeans(iris[, 1:4], centers = 3)
table(km$cluster, iris$Species)
# Ordination
data(dune)
nmds <- metaMDS(dune, distance = "bray", trace = 0)
plot(nmds)
If all three blocks run without errors and produce the expected output/plots, your setup is ready for the course.