# R - Function

Functions are stored as R objects with the class function

Functions can be:

• used as arguments of other functions
• nested

## Syntax

``````function( arglist ) expr
return(value)

#of

f <- function(arglist) {
## Body
}```
```

where:

• arglist is a list of argument including the three dot argument.

Ex: arg1, arg2 = 1, arg3 = 'Nico', arg4 = NULL, …

## Argument

### Arguments mapping

Arguments can be matched positionally or by name.

Arguments mappings works as follow:

• an exact match on the name occurs. The matched argument are removed from the argument list.
• a partial match on the name occurs. The matched argument are removed from the argument list.
• a positional match occurs on the position (the remaining unnamed arguments taking their position number from the remaining argument list)

All the below function calls are equivalent:

``````mean(x=data,na.rm=FALSE)
mean(na.rm=FALSE,x=data)
mean(data,na.rm=FALSE)
mean(na.rm=FALSE,data)```
```

### Args Function

To get the arguments of a function you can use the args function:

````args(data.frame)`
```
``````function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
stringsAsFactors = default.stringsAsFactors())
NULL```
```

### Lazy evaluation

Arguments to functions are evaluated lazily (ie only when needed).

• Argument is not used in the function
``````f = function(x, y) {
return(x*2)
}
f(2)```
```
```` 4`
```
• Argument is not called
``````f = function(x, y) {
print(x)
print(y)
}
f(2)```
```
`````` 2
Error in print(y) : argument "y" is missing, with no default```
```

Notice that the error occurs only when the y argument is needed (ie interpreted)

### The … argument

The … argument indicates a variable number of arguments.

This special argument is used :

• when extending (wrapping) an existing function
``````myPlot = function(x, y, type = "l", ...) {
plot(x, y, type = type, ...)
}```
```
• when the number of arguments cannot be known in advance.
• by generic functions

Any arguments that appear after it must be named explicitly and cannot be partially matched.

## Return Value

The return value of a function is:

• returned by the return function
• of by the last expression in the body function to be evaluated.
``````f = function(x, y) {
return(x*2)
}```
```

is equivalent to:

``````f = function(x, y) {
x*2
}```
```

is equivalent to:

``````f = function(x, y) {
return(x*2)
x*4 # This statement will be skipped
}```
```

## Environment

Typically, a function is defined in the global environment, so that the values of free variables are just found in the user’s workspace

In this case the environment in which a function is defined is the body of another function! ????

## How to

### Get help on function

Just use the question mark ? to obtain the documentation on a function

````?data.frame`
```

### Get the parameters of a function

Use the str function

Example:

``````str(vector)
function (mode = "logical", length = 0L)  ```
```

where:

• function indicates that vector is a function
• (mode = “logical”, length = 0L) are the parameters of the function.

### Get the code of the function

Just type the function:

Example with the function ddply from plyr

``````library(plyr)
> ddply
function (.data, .variables, .fun = NULL, ..., .progress = "none",
.inform = FALSE, .drop = TRUE, .parallel = FALSE, .paropts = NULL)
{
if (empty(.data))
return(.data)
.variables <- as.quoted(.variables)
pieces <- splitter_d(.data, .variables, drop = .drop)
ldply(.data = pieces, .fun = .fun, ..., .progress = .progress,
.inform = .inform, .parallel = .parallel, .paropts = .paropts)
}
<environment: namespace:plyr>```
```

where:

## Support

### Error: could not find function

To avoid the below message (function)

````Error: could not find function "...."`
```

load the package that contains the function

````require(myPackage)`
```

with R - Require or R - Library