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

From: Peter Dalgaard <P.Dalgaard_at_biostat.ku.dk>
Date: Wed, 03 Jun 2009 12:24:27 +0200

Stavros Macrakis wrote:
> 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.

I think it's a feature. There's no way of telling which "..." arguments to vectorize over, and there are arguments that cannot be vectorized, so vectorizing over all of them by default looks like a bad idea. The generic case is that the extra arguments could be things like na.action=na.omit, which you just want to pass through to the scalar function.

If you need the full feature set of mapply, use mapply. The documentation looks like it could be improved, though.

        -p

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

-- 
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk)              FAX: (+45) 35327907

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 03 Jun 2009 - 10:32:33 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 - 10:35:05 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