A function is a block of code organized together to perform a specific task and only runs when it is called. It can have parameters and can return a result.
Automate common and repetitive tasks
Increase reproducibility and readability of the code
Advantages1
Same code reused twice
(do not repeat yourself)
and/or
Reduce code complexity
(workflow modularity)
and/or
Improve code organisation
(workflow readability)
function()Always save your functions in the folder R/1
1. Define the function
Do not forget to source the file each time you modify the function
Functions exit in two ways
Success
i.e. return a value w/ return()
Failure
i.e. throw an error w/ stop()
A function only returns one single object
In most cases, you can omit the keyword return(): this is called an implicit return
In most cases, you can omit the keyword return(): this is called an implicit return
But sometimes, you need an explicit return
Not all functions necessarily return an object
For instance,
graphics) In that case, the function will return NULL
Sometimes, you might want to make the function’s return invisible if it is not assigned to a variable with the function invisible()
This behaviour is usually used:
NULLIf you don’t assigne the output of the function, it will no longer be accessible afterwards
If you implement an invisible return, you have to store the output of the function in a variable and print this variable
# Define the function ----
save_as_csv <- function(data) {
file_name <- here::here("outputs", "filename.csv")
write.csv(data, file_name, row.names = FALSE)
invisible(file_name)
}
# Call the function (no assignation, no return) ----
save_as_csv(data = tab)
# Call the function (w/ assignation) ----
path <- save_as_csv(data = tab)
path
## [1] '/home/user/documents/project_a/outputs/filename.csv' A function can have no argument
A function can have no argument
A function can have no argument
A function can have many arguments
A function can have no argument
A function can have many arguments
Argument order matters
Create pipeable functions
# Define the function ----
subset_data <- function(data, column) {
data[, column, drop = FALSE]
}
data(iris)
# Call the function ----
subset_data(data = iris, column = "Petal.Length")
# Call the function (pipe version) ----
iris |>
subset_data(column = "Petal.Length")
# Call the function (pipe version) ----
iris %>%
subset_data(column = "Petal.Length") Object created inside a function (local environment) is not accessible from the global environment
Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## %Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Convert into function - v0.1
download_gadm <- function() {
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
}Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Clean comments - v0.2
download_gadm <- function() {
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
full_url <- paste0(base_url, file_name)
# Destination folder ----
dest_dir <- here::here("data")
# Destination file ----
full_path <- file.path(dest_dir, file_name)
# Download file ----
download.file(url = full_url, destfile = full_path, mode = "wb")
}Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Function return - v0.3
download_gadm <- function() {
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
full_url <- paste0(base_url, file_name)
# Destination folder ----
dest_dir <- here::here("data")
# Destination file ----
full_path <- file.path(dest_dir, file_name)
# Download file ----
download.file(url = full_url, destfile = full_path, mode = "wb")
full_path
}Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Invisible return - v0.4
download_gadm <- function() {
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
full_url <- paste0(base_url, file_name)
# Destination folder ----
dest_dir <- here::here("data")
# Destination file ----
full_path <- file.path(dest_dir, file_name)
# Download file ----
download.file(url = full_url, destfile = full_path, mode = "wb")
invisible(full_path)
}Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Add ‘file_name’ argument - v0.5
download_gadm <- function(file_name = "gadm41_FRA.gpkg") {
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
#
# Full URL ----
full_url <- paste0(base_url, file_name)
# Destination folder ----
dest_dir <- here::here("data")
# Destination file ----
full_path <- file.path(dest_dir, file_name)
# Download file ----
download.file(url = full_url, destfile = full_path, mode = "wb")
invisible(full_path)
}Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Rename ‘file_name’ argument - v0.6
download_gadm <- function(country = "FRA") {
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- paste0("gadm41_", country, ".gpkg")
# Full URL ----
full_url <- paste0(base_url, file_name)
# Destination folder ----
dest_dir <- here::here("data")
# Destination file ----
full_path <- file.path(dest_dir, file_name)
# Download file ----
download.file(url = full_url, destfile = full_path, mode = "wb")
invisible(full_path)
}Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Add ‘dest_dir’ argument - v0.7
download_gadm <- function(
country = "FRA",
dest_dir = here::here("data")
) {
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- paste0("gadm41_", country, ".gpkg")
# Full URL ----
full_url <- paste0(base_url, file_name)
# Destination file ----
full_path <- file.path(dest_dir, file_name)
# Download file ----
download.file(url = full_url, destfile = full_path, mode = "wb")
invisible(full_path)
}Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Add ‘dest_dir’ argument - v0.7
download_gadm <- function(
country = "FRA",
dest_dir = here::here("data")
) {
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- paste0("gadm41_", country, ".gpkg")
# Full URL ----
full_url <- paste0(base_url, file_name)
# Destination file ----
full_path <- file.path(dest_dir, file_name)
# Download file ----
download.file(url = full_url, destfile = full_path, mode = "wb")
invisible(full_path)
}Easy to use now!
Let’s download the spatial boundary of France
## %
##
## DISTANT FILE
##
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- "gadm41_FRA.gpkg"
# Full URL ----
(full_url <- paste0(base_url, file_name))
# https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/gadm41_FRA.gpkg
##
## LOCAL FILE
##
# Destination folder ----
(dest_dir <- here::here("data"))
# /home/nicolas/projects/demo/data
# Destination file ----
(full_path <- file.path(dest_dir, file_name))
# /home/nicolas/projects/demo/data/gadm41_FRA.gpkg
##
## DOWNLOAD FILE
##
download.file(url = full_url, destfile = full_path, mode = "wb")
## % Add ‘dest_dir’ argument - v0.7
download_gadm <- function(
country = "FRA",
dest_dir = here::here("data")
) {
# Base URL ----
base_url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/gpkg/"
# File name ----
file_name <- paste0("gadm41_", country, ".gpkg")
# Full URL ----
full_url <- paste0(base_url, file_name)
# Destination file ----
full_path <- file.path(dest_dir, file_name)
# Download file ----
download.file(url = full_url, destfile = full_path, mode = "wb")
invisible(full_path)
}To go further:


#' @field value.Rd files (in man/) and NAMESPACE w/ devtools::document() Get started w/ roxygen2: here
#' Compute the arithmetic mean
#'
#' @description
#' This function computes the arithmetic mean of a numeric variable.
#'
#' @param x a `numeric` vector
#'
#' @return A `numeric` value representing the arithmetic mean of `x`.
#'
#' @export
#'
#' @examples
#' x <- 1:10
#' arithmetic_mean(x)
arithmetic_mean <- function(x) {
sum(x) / length(x)
}