[Rd] Vectorize fails for function with ... arglist

Stavros Macrakis
2009-06-03T03:07:10Z

Vectorize is defined to return a function that acts as if 'mapply' was
called.

So we have:

> mapply(dput,1:2)            # mapply form
1L                                      # calls dput on each element of 1:2
2L
[1] 1 2
> Vectorize(dput)(1:2)        # Vectorize form
1L                                        # same behavior
2L
[1] 1 2

Same thing with a named argument:

> mapply(function(a)dput(a),1:2)
1L
2L
[1] 1 2
> Vectorize(function(a)dput(a))(1:2)
1L
2L
[1] 1 2

But though mapply has no problem with function(...):

> mapply(function(...)dput(list(...)),1:2)
list(1L)
list(2L)
[[1]]
[1] 1

[[2]]
[1] 2

> mapply(function(...)dput(list(...)),1:2,11:12)
list(1L, 11L)
list(2L, 12L)
     [,1] [,2]
[1,] 1    2
[2,] 11   12

Vectorize fails silently in this case:

> Vectorize(function(...)dput(list(...))(1:2)
list(1:2)                    # calls dput with entire vector
                                # invisible result inherited from dput

> Vectorize(function(...)dput(list(...)))(1:2,11:12)
list(1:2, 11:12)

and sure enough:

> Vectorize(function(...)list(...))
function(...)list(...)                            # returns arg unmodified!

I looked at the code, and ... args are *explicitly* rejected. I see no
logical reason for this inconsistency, and the documentation doesn't require
it.

              -s

PS This is not an artificial example concocted to demonstrate
inconsistencies.  I had written the following function which wraps another
function in a tryCatch:

       catcher <- function(f) function(...)
tryCatch(do.call(f,list(...)),error=function(e) NA)

(The '...' argument list allows this to work with a function of any
number
of arguments.)

but instead of catching individual errors in
Vectorize(catcher(fun))(1:10,1:10), it caught them all as one big error,
which was not at all the goal.

	[[alternative HTML version deleted]]