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

From: Stavros Macrakis <macrakis_at_alum.mit.edu>
Date: Tue, 02 Jun 2009 23:07:10 -0400


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]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 03 Jun 2009 - 03:12:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 03 Jun 2009 - 12:34:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive