| function | R Documentation |
These functions provide the base mechanisms for definingnew functions in theR language.
function( arglist ) expr\( arglist ) exprreturn(value)
arglist | Empty or one or more name or name=expression terms. |
expr | An expression. |
value | An expression. |
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.
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.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
args.
formals,body andenvironment for accessing the component parts of afunction.
debug for debugging; usinginvisible insidereturn(.) for returninginvisibly.
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)Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.
