Movatterモバイル変換


[0]ホーム

URL:


rdrr.io

function: Function Definition

functionR Documentation

Function Definition

Description

These functions provide the base mechanisms for definingnew functions in theR language.

Usage

function( arglist ) expr\( arglist ) exprreturn(value)

Arguments

arglist

Empty or one or more name or name=expression terms.

expr

An expression.

value

An expression.

Details

The names in an argument list can be back-quoted non-standard names(see ‘backquote’).

Ifvalue is missing,NULL is returned. If it is asingle expression, the value of the evaluated expression is returned.(The expression is evaluated as soon asreturn is called, inthe evaluation frame of the function and before anyon.exit expression is evaluated.)

If the end of a function is reached without callingreturn, thevalue of the last evaluated expression is returned.

The shorthand form\(x) x + 1 is parsed asfunction(x) x + 1. It may be helpful in making code containing simple functionexpressions more readable.

Technical details

This type of function is not the only type inR: they are calledclosures (a name with origins in LISP) to distinguish them fromprimitive functions.

A closure has three components, itsformals (its argumentlist), itsbody (expr in the ‘Usage’section) and itsenvironment which provides theenclosure of the evaluation frame when the closure is used.

There is an optional further component if the closure has beenbyte-compiled. This is not normally user-visible, but is indicatedwhen functions are printed.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.

See Also

args.

formals,body andenvironment for accessing the component parts of afunction.

debug for debugging; usinginvisible insidereturn(.) for returninginvisibly.

Examples

norm <- function(x) sqrt(x%*%x)norm(1:4)## An anonymous function:(function(x, y){ z <- x^2 + y^2; x+y+z })(0:7, 1)

What can we improve?

R Package Documentation

Browse R Packages

We want your feedback!

Note that we can't provide technical support on individual packages. You should contact the package authors for that.

 
Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, readEmbedding Snippets.

Close

[8]ページ先頭

©2009-2025 Movatter.jp