Setup

How to install R and set up a working environment

The tutorials rely on R, so you need to have R installed on your computer and ideally an Integrated Development Environment (IDE), for instance RStudio, Visual Studio Code or Positron. Please have a look at the tutorial on how to setup a working environment for scientific computing with R: https://frbcesab.github.io/rsetup/

R packages needed for these tutorials

We will mostly use terra(Hijmans 2025), sf(Pebesma 2018), mapview(Appelhans et al. 2025), and exactextract.

needed_packages <- c("terra", "sf", "mapview", "exactextract")

check_and_install <- function(x) {
  if (!requireNamespace(x, quietly = TRUE)) {
    install.packages(x)
  }
}

invisible(lapply(needed_packages, check_and_install))
Installing package into '/home/romain/R/x86_64-pc-linux-gnu-library/4.5'
(as 'lib' is unspecified)
Warning: package 'exactextract' is not available for this version of R

A version of this package for your version of R might be available elsewhere,
see the ideas at
https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

Download needed dataset

As an toy dataset, the tutorials rely on observed occurrence of otter in 2021 around Montpellier, France. These dataset were downloaded directly from R, see the tutorial on creating a toy dataset from GBIF, IGN and CHELSA for more details.

You can run the following commands to download the dataset needed for the workshop (total size : 11Mb).

# set data directory
datadir <- here::here("data")
if (!dir.exists(datadir)) {
  dir.create(
    path = datadir,
    showWarnings = FALSE,
    recursive = TRUE
  )
}

# download base url
url_git <- "https://github.com/FRBCesab/spatial-r/raw/refs/heads/main/data"

# download GBIF data (20kb)
file1 <- "gbif_otter_2021_mpl50km.csv"
download.file(file.path(url_git, file1), file.path(datadir, file1), mode = "wb")

# download elevation data (500kb)
file2 <- "BDALTI_mpl50km.tif"
download.file(file.path(url_git, file2), file.path(datadir, file2), mode = "wb")

# download administrative borders (2.5Mb)
file3 <- "BDCARTO-Commune_mpl50km.gpkg"
download.file(file.path(url_git, file3), file.path(datadir, file3), mode = "wb")

# download rivers (2Mb)
file4 <- "BDCARTO-River_mpl50km.gpkg"
download.file(file.path(url_git, file4), file.path(datadir, file4), mode = "wb")

# download land cover (3.7Mb)
file5 <- "BDCARTO-LULC_mpl50km"
shpext <- c("dbf", "prj", "shp", "shx")
url_shp <- file.path(url_git, paste(file5, shpext, sep = "."))
dir_shp <- file.path(datadir, paste(file5, shpext, sep = "."))
for (i in seq_along(shpext)) {
  download.file(url_shp[i], dir_shp[i], mode = "wb")
}

# download CHELSA monthly temperature (2.2Mb)
file6 <- "CHELSA_monthly_tas_2015_2021.tif"
download.file(file.path(url_git, file6), file.path(datadir, file6), mode = "wb")