If you have any trouble, contact me: aurele.toussaint@cnrs.fr
Installing R on Windows 10/11 is
straightforward.
The easiest way is to download it from CRAN.
For Mac users:
R-4.5.1.pkg
(x86_64)R-4.5.1-arm64.pkg
For Linux users:
Click Download R for Linux on CRAN.
Follow the instructions for your distribution.
Example for Ubuntu/Debian:
sudo apt update
sudo apt install r-base
R is the engine, but RStudio (now Posit RStudio) makes it easier to use.
If everything is working, open RStudio and you should see a window similar to this:
RStudio has four main panels (by default):
➡️ You should always work in a script
(File → New File → R Script
) and run code with
- Ctrl + Enter
(Windows)
- Cmd + Enter
(Mac).
Try a few basic commands:
2 + 2
## [1] 4
x <- 5
y <- c(1, 2, 3, 4, 5)
mean(y)
## [1] 3
plot(y)
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
Many functions are in packages.
install.packages("ade4") # ecological analyses
install.packages(c("tidyverse", "ggplot2", "dplyr")) # useful tools
library(ade4)
library(ggplot2)
update.packages(ask = FALSE)
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
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")
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()
?mean
or help(mean)
→ function
documentation.example(mean)
→ run examples.sessionInfo()
→ useful when asking for help (shows R,
OS, and package versions).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(...) |
.R
).#
.data/
, scripts/
,
results/
.setwd()
.Session → Restart R
) when things
behave strangely.snake_case
for
files and variables.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.