Function Argument Lists and missing()

This entry is part 17 of 21 in the series Using R

Sometimes it is useful to write a wrapper function for an existing function. In this short example we demonstrate how to grab the list of arguments passed to a function and use it to call another function, taking care of optional arguments with or without default values.

Authors of R functions often specify default values for function arguments.  Invocation of the function may override defaults for some arguments and accept defaults for others. Inside of a function with default argument values, arguments always have a value even if it is NA or NULL — they are never ‘missing’.

Sanity checks in such functions often test arguments before they are used as in the following example:

A few quick tests show how this works:

So far so good.

But what happens when the function author doesn’t provide a default value for optional arguments and instead uses R’s missing() function to to determine whether the optional argument was specified?

Here things look different:

The output is equally correct though perhaps not as useful.

In general, creating functions where every optional argument has a default value of NULL is the recommended practice. However, sometimes we want to write a wrapper function for another function that does not adhere to this practice. In this case we need to convert our set of incoming arguments with some potential NULLs into a new set where some arguments are missing.

To accomplish this we will use the function to obtain a list of arguments when the function was called (throwing away the first item from which is the function name). We can then modify this list of actual arguments used to include what the wrapper function considers important default values.

The following example shows how to handle this:

As the authors of the wrapper function, we specify non-NULL defaults for certain arguments. We will enforce that these arguments get included in the argList whether or not they were specified on the command line. Any arguments whose value is NULL will not appear in argList and will therefore be ‘missing’ when we,argList) :

Note how we pass on the default value of a='A'  but still allow a user to set it to NULL in case the missing() test in f2() is important.

In general, function authors should avoid the missing() construct and should always specify a default value of NULL for optional arguments. This allows for more systematic sanity checking and passing of arguments to other functions. But there is a huge body of code out there that tests for missing() arguments and we need to know how work with it.

Best of luck writing robust, debuggable functions!

Series NavigationMazamaSpatialUtils — Ebola Map ExampleVisualizing Bikeshare Data
This entry was posted in R. Bookmark the permalink.

Comments are closed.