(R-intro.info)Writing your own functions
10 Writing your own functions
*****************************
As we have seen informally along the way, the R language allows the user
to create objects of mode _function_. These are true R functions that
are stored in a special internal form and may be used in further
expressions and so on. In the process, the language gains enormously in
power, convenience and elegance, and learning to write useful functions
is one of the main ways to make your use of R comfortable and
productive.
It should be emphasized that most of the functions supplied as part
of the R system, such as 'mean()', 'var()', 'postscript()' and so on,
are themselves written in R and thus do not differ materially from user
written functions.
A function is defined by an assignment of the form
> NAME <- function(ARG_1, ARG_2, ...) EXPRESSION
The EXPRESSION is an R expression, (usually a grouped expression), that
uses the arguments, ARG_I, to calculate a value. The value of the
expression is the value returned for the function.
A call to the function then usually takes the form 'NAME(EXPR_1,
EXPR_2, ...)' and may occur anywhere a function call is legitimate.
- Simple examples
- Defining new binary operators
- Named arguments and defaults
- The three dots argument
- Assignment within functions
- More advanced examples
- Scope
- Customizing the environment
- Object orientation
automatically generated by info2www version 1.2.2.9