R - Function

1 - About

Functions are stored as R objects with the class function

Functions can be:

  • used as arguments of other functions
  • nested

3 - Syntax

function( arglist ) expr


f <- function(arglist) {
   ## Body


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

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

4 - Argument

4.1 - 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:


4.2 - Args Function

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


function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, 
    stringsAsFactors = default.stringsAsFactors()) 

4.3 - Lazy evaluation

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

  • Argument is not used in the function

f = function(x, y) {

[1] 4

  • Argument is not called

f = function(x, y) {

[1] 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)

4.4 - 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.

5 - 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) {

is equivalent to:

f = function(x, y) {

is equivalent to:

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

6 - 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! ????

7 - How to

7.1 - Get help on function

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


7.2 - Get the parameters of a function

Use the str function


function (mode = "logical", length = 0L)  


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

7.3 - Get the code of the function

Just type the function:

Example with the function ddply from plyr

> ddply
function (.data, .variables, .fun = NULL, ..., .progress = "none", 
    .inform = FALSE, .drop = TRUE, .parallel = FALSE, .paropts = NULL) 
    if (empty(.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>


8 - Support

8.1 - Error: could not find function

To avoid the below message (function)

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

load the package that contains the function


with R - Require or R - Library

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap