Passer des arguments entre fonctions

Introduction à l'écriture de fonctions en R

Richie Cotton

Data Evangelist at DataCamp

Calculer la moyenne géométrique

x %>%
  log() %>%
  mean() %>%
  exp()
Introduction à l'écriture de fonctions en R

En faire une fonction

calc_geometric_mean <- function(x) {
  x %>%
    log() %>%
    mean() %>%
    exp()
}
Introduction à l'écriture de fonctions en R

Gérer les valeurs manquantes

calc_geometric_mean <- function(x, na.rm = FALSE) {
  x %>%
    log() %>%
    mean(na.rm = na.rm) %>%
    exp()
}
Introduction à l'écriture de fonctions en R

Utiliser ...

calc_geometric_mean <- function(x, ...) {
  x %>%
    log() %>%
    mean(...) %>%
    exp()
}
Introduction à l'écriture de fonctions en R

Le compromis

Avantages

  • Moins de saisie pour vous
  • Pas besoin d'aligner les signatures

Inconvénients

  • Il faut faire confiance à la fonction interne
  • L'interface est moins évidente pour les utilisateurs
Introduction à l'écriture de fonctions en R

Passons à la pratique !

Introduction à l'écriture de fonctions en R

Preparing Video For Download...