[Rd] Ellipsis to Two Functions When One Has Nested Functions

Duncan Murdoch
Wed, 28 May 2014 11:34:40 -0000

On 28/05/2014, 2:00 AM, Dario Strbenac wrote:
> Hello
>
> If I have a function
>
> aFunction <- function(data, alpha, ...)
> {
>      transform(alpha, ...)
>      rowMeans(data) > alpha
> }
>
> f <- function(data, selection, ...)
> {
>      selected <- selection(data, ...)
>      plot(data[selected, ], ...)
> }
>
> f(aDataset, aFunction, alpha = 10, pch = 19, transform = sqrt)
>
> and selection calls another function, which has ... and that function calls
other functions, is there an easier way than getting the formals of all the
functions called internally by selection, and all of the formals of functions
called by plot ? Is there any option to change the error into a warning message
?

You can use try() to catch an error, but you can't ignore it.  The code
that triggered the error will still exit at that point.

There are at least two common ways of handling the situation where you
want to pass optional args to two different functions.  One is to use

args <- list(...)

to get all the arguments in a list, and then manually split up the list,
and use do.call() to pass some of them on.  Generally you would do this
only if you knew in advance how to split the args, you wouldn't try to
call formals() to work it out at run-time.

Another way is to have an argument like "control" in optim(), and ask
the user to pass one set of options via that, and the other via ... .
You'd use do.call() again to construct that call.

Duncan Murdoch