If you have any trouble, contact me:


Part 1. Install R

Windows

Installing R on Windows 10/11 is straightforward.
The easiest way is to download it from CRAN.

  1. Click Download R for Windows.
  2. Then select base and click on install R for the first time.
  3. Download the latest version (e.g. R 4.5.1, as of 09/09/2025).

Mac

For Mac users:

  1. Click Download R for (Mac) OS X.
  2. Select the installer that matches your system:
    • Intel MacsR-4.5.1.pkg (x86_64)
    • Apple Silicon (M1/M2/M3)R-4.5.1-arm64.pkg
  3. Follow the installation instructions.

Linux

For Linux users:

  1. Click Download R for Linux on CRAN.

  2. Follow the instructions for your distribution.
    Example for Ubuntu/Debian:

    sudo apt update
    sudo apt install r-base

Part 2. Install RStudio (Posit RStudio)

R is the engine, but RStudio (now Posit RStudio) makes it easier to use.

  1. Go to the RStudio website.
  2. Download the FREE version.
  3. Select your operating system (Windows, Mac, Linux) and install.

If everything is working, open RStudio and you should see a window similar to this:


Part 3. First Steps in RStudio

RStudio has four main panels (by default):

  1. Console (bottom left) → run commands here.
  2. Script editor (top left) → write and save your code.
  3. Environment/History (top right) → see your variables.
  4. Files/Plots/Packages/Help (bottom right).

➡️ You should always work in a script (File → New File → R Script) and run code with
- Ctrl + Enter (Windows)
- Cmd + Enter (Mac).


Part 4. Your First R Commands

Try a few basic commands:

2 + 2
## [1] 4
x <- 5
y <- c(1, 2, 3, 4, 5)
mean(y)
## [1] 3
plot(y)


Part 5. Working with Directories and Projects

It is best practice to use RStudio Projects:
- Go to File → New Project.
- Create a folder for your work.
- All scripts and data should go inside this folder.

Check where you are:

getwd()
## [1] "C:/Users/touss/OneDrive/iCloud Drive/00_courses/2020_LTOM/Rdown"

Set a working directory manually (less recommended):

setwd("C:/Users/YourName/Documents/myproject")

For robust file paths, consider the here package:

install.packages("here")
library(here)
here("data", "myfile.csv")  # builds a path inside your project

Part 6. Installing and Loading Packages

Many functions are in packages.

  • Install once:
install.packages("ade4") # ecological analyses
install.packages(c("tidyverse", "ggplot2", "dplyr")) # useful tools
  • Load every session:
library(ade4)
library(ggplot2)
  • Update packages:
update.packages(ask = FALSE)
  • List installed packages:
head(installed.packages()[, 1:2])
##            Package      LibPath                                         
## abind      "abind"      "C:/Users/touss/AppData/Local/R/win-library/4.4"
## ade4       "ade4"       "C:/Users/touss/AppData/Local/R/win-library/4.4"
## ape        "ape"        "C:/Users/touss/AppData/Local/R/win-library/4.4"
## askpass    "askpass"    "C:/Users/touss/AppData/Local/R/win-library/4.4"
## assertthat "assertthat" "C:/Users/touss/AppData/Local/R/win-library/4.4"
## backports  "backports"  "C:/Users/touss/AppData/Local/R/win-library/4.4"

On Windows, some packages require Rtools: https://cran.r-project.org/bin/windows/Rtools/
On Mac, some packages require Xcode Command Line Tools:

xcode-select --install

Part 7. Importing Data

The first step in most projects is to load data.

Example: Reading a CSV file.

mydata <- read.csv("data/myfile.csv")
head(mydata)

For Excel files:

install.packages("readxl")
library(readxl)
mydata <- read_excel("data/myfile.xlsx")
head(mydata)

Using tidyverse readr (fast CSV import):

install.packages("readr")
library(readr)
mydata <- read_csv("data/myfile.csv")

Part 8. Plotting Your First Graphs

R comes with built-in datasets, e.g. iris.

Base R:

head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
plot(iris$Sepal.Length, iris$Sepal.Width,
     main = "Sepal Length vs Width",
     xlab = "Sepal Length", ylab = "Sepal Width")

With ggplot2:

library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  theme_minimal()

Part 9. Getting Help

  • ?mean or help(mean) → function documentation.
  • example(mean) → run examples.
  • Posit Cheat Sheets → highly recommended.
  • sessionInfo() → useful when asking for help (shows R, OS, and package versions).

Part 10. Common Errors

Error message Likely cause Solution
there is no package called 'xxx' Package not installed Run install.packages("xxx")
could not find function "foo" Package not loaded Run library(package_name)
Error: object 'x' not found Variable not defined Assign with <- before using it
Error in setwd(...) Wrong file path Use RStudio Projects or here::here(...)

Part 11. Good Practices from Day 1

  • Always work in scripts and save them (.R).
  • Comment code with #.
  • Organize folders: data/, scripts/, results/.
  • Use Projects, not manual setwd().
  • Restart RStudio (Session → Restart R) when things behave strangely.
  • Prefer readable, consistent naming: snake_case for files and variables.

Part 12. Quick Workflow Summary

  1. Install R (from CRAN).
  2. Install RStudio (from Posit).
  3. Create a Project.
  4. Write code in a script.
  5. Install packages (once).
  6. Load packages (each session).
  7. Import data.
  8. Run analysis and plots.
  9. Save results and scripts.

Part 13. FAQ

  • Error: there is no package called 'xxx'
    → You forgot to install it: install.packages("xxx").

  • Error: Compilation failed on Windows
    → Install Rtools.

  • Error: Compilation failed on Mac
    → Install Xcode Command Line Tools.