problem with replicate and "..." (PR#8472)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

problem with replicate and "..." (PR#8472)

Jason Eisner
I am using R version 2.0.0 (2004-10-04) on Fedora Core 2.

This works correctly:

> foo <- function(x=1,y=2) { c(x,y) }
> bar <- function(n,...) c(n,foo(...))
> bar(10,3)
[1] 10  3  2

But it goes wrong if I replace "c" in bar with "replicate":

> foo <- function(x=1,y=2) { c(x,y) }
> bar <- function(n,...) replicate(n,foo(...))
> bar(10,3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    0    0    0    0    0    0    0    0     0
[2,]    2    2    2    2    2    2    2    2    2     2

It is mysterious why x was bound to the apparently arbitrary
value 0 while y was left at its default.

The ... arguments to bar seems to be ignored altogether.
bar(10), bar(10,x=3), and bar(10,3,4) give the same result.
Furthermore, bar(10,extra=3) does not give an error.

Perhaps this mysterious behavior is unavoidable because of
the kind of hack replicate is?

Thanks ...

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: problem with replicate and "..." (PR#8472)

Peter Dalgaard
[hidden email] writes:

> I am using R version 2.0.0 (2004-10-04) on Fedora Core 2.
>
> This works correctly:
>
> > foo <- function(x=1,y=2) { c(x,y) }
> > bar <- function(n,...) c(n,foo(...))
> > bar(10,3)
> [1] 10  3  2
>
> But it goes wrong if I replace "c" in bar with "replicate":
>
> > foo <- function(x=1,y=2) { c(x,y) }
> > bar <- function(n,...) replicate(n,foo(...))
> > bar(10,3)
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
> [1,]    0    0    0    0    0    0    0    0    0     0
> [2,]    2    2    2    2    2    2    2    2    2     2
>
> It is mysterious why x was bound to the apparently arbitrary
> value 0 while y was left at its default.
>
> The ... arguments to bar seems to be ignored altogether.
> bar(10), bar(10,x=3), and bar(10,3,4) give the same result.
> Furthermore, bar(10,extra=3) does not give an error.
>
> Perhaps this mysterious behavior is unavoidable because of
> the kind of hack replicate is?

Yes. It is really a wrapper for

sapply(integer(n), eval.parent(substitute(function(...) expr))

Now, integer(n) is n zeroes, and the function that is passed to sapply
is

Browse[1]> FUN
function (...)
foo(...)
<environment: 0xd82338>

Now, this gets called as FUN(0) and in turn foo(0) which is c(0,2).

So, the short answer is "don't do that", and the long answer is "don't
do that". If you're adventurous, you could try experimenting with a
different definition, possibly

sapply(integer(n), eval.parent(substitute(function(...) eval.parent(expr)))

but I'm far from sure that it works...

--
   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
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: problem with replicate and "..." (PR#8472)

Jason Eisner
Yesterday morning, Peter Dalgaard wrote:

> [hidden email] writes:
>
>> I am using R version 2.0.0 (2004-10-04) on Fedora Core 2.
>>
>> This works correctly:
>>
>> > foo <- function(x=1,y=2) { c(x,y) }
>> > bar <- function(n,...) c(n,foo(...))
>> > bar(10,3)
>> [1] 10  3  2
>>
>> But it goes wrong if I replace "c" in bar with "replicate":
>>
>> > foo <- function(x=1,y=2) { c(x,y) }
>> > bar <- function(n,...) replicate(n,foo(...))
>> > bar(10,3)
>>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>> [1,]    0    0    0    0    0    0    0    0    0     0
>> [2,]    2    2    2    2    2    2    2    2    2     2
>>
>> It is mysterious why x was bound to the apparently arbitrary
>> value 0 while y was left at its default.
>>
>> The ... arguments to bar seems to be ignored altogether.
>> bar(10), bar(10,x=3), and bar(10,3,4) give the same result.
>> Furthermore, bar(10,extra=3) does not give an error.
>>
>> Perhaps this mysterious behavior is unavoidable because of
>> the kind of hack replicate is?
>
> Yes. It is really a wrapper for
>
> sapply(integer(n), eval.parent(substitute(function(...) expr))
>
> Now, integer(n) is n zeroes, and the function that is passed to sapply
> is
>
> Browse[1]> FUN
> function (...)
> foo(...)
> <environment: 0xd82338>
>
> Now, this gets called as FUN(0) and in turn foo(0) which is c(0,2).
>
> So, the short answer is "don't do that", and the long answer is "don't
> do that". If you're adventurous, you could try experimenting with a
> different definition, possibly
>
> sapply(integer(n), eval.parent(substitute(function(...) eval.parent(expr)))
>
> but I'm far from sure that it works...

Peter: thanks for the good explanation.

Perhaps the OFFICIAL replicate function can be fixed as you suggest
above, or by somehow incorporating this workaround:

   bar <- function(n,...) { f <- function() foo(...);
                            replicate(n,f()) }


If not, then may I suggest that help("replicate") should document the
limitation, and perhaps the workaround as well?  

(The help page does mention that replicate is just a convenience
wrapper, but without a BUGS or LIMITATIONS section as on Unix
manpages, a user might be forgiven for assuming that it actually works
in all cases.  Obviously, a user shouldn't have to understand how a
function is implemented in order to avoid nasty special cases.)

Thanks!  -jason

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel