Named arguments and defaults. ``...''



next up previous contents
Next: Assignments within functions Up: Writing your own Previous: Defining new binary

Named arguments and defaults. ``...''

As first noted in §gif if arguments to called functions are given in the ``name=object'' form, they may be given in any order. Furthermore the argument sequence may begin in the unnamed, positional form, and specify named arguments after the positional arguments.

Thus if there is a function fun1 defined by

fun1 <- function(data, data.frame, graph, limit) {... gif }

Then the function may be invoked in several ways, for example

ans <- fun1(d, df, 20, T)

ans <- fun1(d, df, graph=T, limit=20)

ans <- fun1(data=d, limit=20, graph=T, data.frame=df)

are all equivalent.

In many cases arguments can be given commonly appropriate default values, in which case they may be omitted altogether from the call when the defaults are appropriate. For example, if fun1 were defined as

fun1 <- function(data, data.frame, graph=T, limit=20) {... gif }

it could be called as

ans <- fun1(d, df)

which is now equivalent to the three cases above, or as

ans <- fun1(d, df, limit=10)

which changes one of the defaults.

It is important to note that defaults may be arbitrary expressions, even involving other arguments to the same function; they are not restricted to be constants as in our simple example here.

Another frequent requirement is to allow one function to pass on argument settings to another. For example many graphics functions use the function par() and functions like plot() allow the user to pass on graphical parameters to par() to control the graphical output. This can be done by including an extra argument, literally ``...'', of the function, which may then be passed on. An outline example is given in Figure 4.

 
Figure 4:   Use of the ellipsis argument, ``...''

Note here that the ellipses, ``...'' are literal S-PLUS, not a typographical device.



Erik Moledor
Tue Jan 31 21:02:18 EST 1995