needed_packages <- c("terra", "sf", "mapview", "here")
check_and_install <- function(x) {
if (!requireNamespace(x, quietly = TRUE)) {
install.packages(x)
}
}
invisible(lapply(needed_packages, check_and_install))Setup
How to set up R
The tutorials rely on , so you need to have it installed on your computer with, ideally, an Integrated Development Environment (IDE) such as RStudio, Visual Studio Code or Positron.
Please have a look at the tutorial on how to setup a working environment for scientific computing with : https://frbcesab.github.io/rsetup/
R packages needed
We will use four packages: terra(Hijmans 2025) and sf(Pebesma 2018) for handling spatial data, mapview(Appelhans et al. 2025) for interactive maps, and here(Müller 2025) for locating files locally. Make sure these four packages are installed locally by running the following script:
Download toy dataset
As a toy dataset, the tutorials rely on observed occurrences of otters in 2021 around Montpellier, France. These datasets were downloaded directly from , for more details see the tutorial on creating a toy dataset from GBIF, IGN and CHELSA.
You can run the following commands to download the dataset needed for the workshop (total size : 11Mb), or download a zip archive
# 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 as csv file (14kb)
file1 <- "gbif_otter_2021_mpl50km.csv"
download.file(file.path(url_git, file1), file.path(datadir, file1), mode = "wb")
# download GBIF data as geopackage file (124kb)
file2 <- "gbif_otter_2021_mpl50km.gpkg"
download.file(file.path(url_git, file2), file.path(datadir, file2), mode = "wb")
# download rivers (2.5Mb)
file3 <- "BDCARTO-River_mpl50km.gpkg"
download.file(file.path(url_git, file3), file.path(datadir, file3), mode = "wb")
# download land cover (4.7Mb)
file4 <- "BDCARTO-LULC_mpl50km"
shpext <- c("dbf", "prj", "shp", "shx")
url_shp <- file.path(url_git, paste(file4, shpext, sep = "."))
dir_shp <- file.path(datadir, paste(file4, shpext, sep = "."))
for (i in seq_along(shpext)) {
download.file(url_shp[i], dir_shp[i], mode = "wb")
}
# download elevation data (505kb)
file5 <- "BDALTI_mpl50km.tif"
download.file(file.path(url_git, file5), file.path(datadir, file5), 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")
# download GADM France country borders (904 kb)
file7 <- "gadm41_FRA_0_pk.rds"
download.file(file.path(url_git, file7), file.path(datadir, file7), mode = "wb")Session information
For reproducibility, the tutorials were last compiled on 13 November 2025 with the following libraries:
R version 4.5.1 (2025-06-13)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 25.10
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.12.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.1; LAPACK version 3.12.0
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] sf_1.0-21 terra_1.8-70 here_1.0.2 mapview_2.11.4
loaded via a namespace (and not attached):
[1] jsonlite_2.0.0 compiler_4.5.1 Rcpp_1.1.0 dichromat_2.0-0.1
[5] leaflet_2.2.3 scales_1.4.0 png_0.1-8 yaml_2.3.10
[9] fastmap_1.2.0 lattice_0.22-7 R6_2.6.1 classInt_0.4-11
[13] satellite_1.0.6 knitr_1.50 htmlwidgets_1.6.4 units_1.0-0
[17] rprojroot_2.1.1 DBI_1.2.3 RColorBrewer_1.1-3 rlang_1.1.6
[21] sp_2.2-0 xfun_0.54 cli_3.6.5 magrittr_2.0.4
[25] class_7.3-23 crosstalk_1.2.2 digest_0.6.37 grid_4.5.1
[29] leafem_0.2.5 base64enc_0.1-3 lifecycle_1.0.4 KernSmooth_2.23-26
[33] proxy_0.4-27 evaluate_1.0.5 glue_1.8.0 raster_3.6-32
[37] farver_2.1.2 codetools_0.2-20 stats4_4.5.1 e1071_1.7-16
[41] rmarkdown_2.30 tools_4.5.1 htmltools_0.5.8.1