Argumenten tussen functies doorgeven

Introductie tot functies schrijven in R

Richie Cotton

Data Evangelist at DataCamp

De geometrische gemiddelde berekenen

x %>%
  log() %>%
  mean() %>%
  exp()
Introductie tot functies schrijven in R

In een functie gieten

calc_geometric_mean <- function(x) {
  x %>%
    log() %>%
    mean() %>%
    exp()
}
Introductie tot functies schrijven in R

Ontbrekende waarden afhandelen

calc_geometric_mean <- function(x, na.rm = FALSE) {
  x %>%
    log() %>%
    mean(na.rm = na.rm) %>%
    exp()
}
Introductie tot functies schrijven in R

... gebruiken

calc_geometric_mean <- function(x, ...) {
  x %>%
    log() %>%
    mean(...) %>%
    exp()
}
Introductie tot functies schrijven in R

De afweging

Voordelen

  • Minder typen voor jou
  • Geen noodzaak om signaturen te laten matchen

Nadelen

  • Je moet de binnenste functie vertrouwen
  • De interface is minder duidelijk voor gebruikers
Introductie tot functies schrijven in R

Laten we oefenen!

Introductie tot functies schrijven in R

Preparing Video For Download...