Re: [R] Recall() and sapply()

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 31 Mar 2005 - 00:21:53 EST

I believe that the function that Recall executes is the function in which Recall, itself, is evaluated -- not the function in which Recall appears. In normal cases these are the same but if you pass Recall to another function then they are not the same. Here Recall is being passed to sapply (which in turn is likely passing it to other functions). Because of lazy evaluation Recall does not get evaluated until it is found within sapply (or a function called by it or called by one called by it, etc.) and at that point its recalling the wrong function. AFAICS one cannot pass Recall to another function.

You could rewrite the expression that uses sapply to use iteration instead or you could do it as shown below. In this example, the use of f2 within supply refers to the inner f2 which does not change even if the name of the outer f2 does.

  f2 <- function(n) {

     f2 <- function(n) if(length(n)>1) sapply(n,f2) else matrix(n,n,n)
     f2(n)

   }
   f3 <- f2
   f2(1:3)
   f3(1:3) # gives same result

On Wed, 30 Mar 2005 09:28:08 +0100, Robin Hankin <r.hankin@soc.soton.ac.uk> wrote:
> Hi.
>
> I'm having difficulty following the advice given in help(Recall).
> Consider the two
> following toy functions:
>
> f1 <- function(n){
> if(length(n)>1){return(sapply(n,f1))}
> matrix(n,n,n)
> }
>
> f2 <- function(n){
> if(length(n)>1){return(sapply(n,Recall))}
> matrix(n,n,n)
> }
>
> f1() works as desired (that is, f(1:3), say, gives me a three element
> list whose i-th element
> is an i-by-i matrix whose elements are all i).
>
> But f2() doesn't.
>
> How do I modify either function to use Recall()? What exactly is
> Recall() calling here?
>
> --
> Robin Hankin
> Uncertainty Analyst
> Southampton Oceanography Centre
> European Way, Southampton SO14 3ZH, UK
> tel 023-8059-7743
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Mar 31 00:29:40 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:57 EST