Functions

Table of Contents

Overview

Any ordinary assignments done within the function are local and temporary and are lost after exit from the function.

# define new binary operator
"%!%" <- function(X, y) { … }
fun1 <- function(data, data.frame, graph, limit) {
  [function body omitted]
}

ans <- fun1(d, df, TRUE, 20)
ans <- fun1(d, df, graph=TRUE, limit=20)
ans <- fun1(data=d, limit=20, graph=TRUE, data.frame=df)
foo <- function(..., x = 100) {
  c(..., x)
}
foo(1, 2, 3)
foo(1, 2, x = 3)

bar <- function(...) {
  c(..1, ..3)
}
bar(1, 2, 3)
[1]   1   2   3 100
[1] 1 2 3
[1] 1 3

Functions: Advanced R tutorial

Function arguments are lazily evaluated discussion

Function arguments are lazy evaluated. Here are some surprises:

As x was not used, stop() was not evaluated. As such, no error occurs
[1] 10
As default arguments are also evaluated lazily, we can use other arguments to define default arguments
[1] 100
We can even use this characteristic as follows:
[1] 100