Předávání argumentů mezi funkcemi

Introduction to Writing Functions in R

Richie Cotton

Data Evangelist at DataCamp

Výpočet geometrického průměru

x %>%
  log() %>%
  mean() %>%
  exp()
Introduction to Writing Functions in R

Zabalení do funkce

calc_geometric_mean <- function(x) {
  x %>%
    log() %>%
    mean() %>%
    exp()
}
Introduction to Writing Functions in R

Zpracování chybějících hodnot

calc_geometric_mean <- function(x, na.rm = FALSE) {
  x %>%
    log() %>%
    mean(na.rm = na.rm) %>%
    exp()
}
Introduction to Writing Functions in R

Použití ...

calc_geometric_mean <- function(x, ...) {
  x %>%
    log() %>%
    mean(...) %>%
    exp()
}
Introduction to Writing Functions in R

Kompromis

Výhody

  • Méně psaní
  • Není nutné slaďovat signatury

Nevýhody

  • Je nutné důvěřovat vnitřní funkci
  • Rozhraní není uživatelům zřejmé
Introduction to Writing Functions in R

Pojďme procvičovat!

Introduction to Writing Functions in R

Preparing Video For Download...