[R] lapply and dynamically linked functions

Liaw, Andy andy_liaw at merck.com
Thu Feb 12 14:11:11 CET 2004


That's the expected behavior, not a bug, because:

> args(lapply)
function (X, FUN, ...) 
NULL

so lapply has `X' as an formal argument.  Any calls to lapply() with named
argument `X=...' will match to that, so the net effect is that the `X=...'
part gets used by lapply() as the list to operate on, rather than pass down
to `FUN'.  You might want to read the relevant section of the `R Language
Definition' manual.

I believe the argument is deliberately named with capital `X' to avoid
collision with `x'.

Andy

> From: Jason Nielsen
> 
> Well I finally figured it out.  After proving to myself that 
> it wasn't the 
> Fortran wrapper with a simple example I started to dig into 
> this.  The 
> problem is that I was naming one of my parameters X... which 
> is the name 
> of the input value in lapply... I'm not sure if this is a bug 
> or just the 
> way it works but here is an example:
> 
> examplegood<-function(kn,XX)
> {
>   out<-sum(kn)*sum(XX)
>   out
> }
> 
> examplebad<-function(kn,X)
> {
>   out<-sum(kn)*sum(X)
>   out
> }
> 
> mylist<-vector("list")
> 
> for(i in 1:5) mylist[[i]]<-i
> 
> a<-1:10
> 
> Now run lapply and you get:
> 
> > lapply(mylist,examplebad,X=a)
> Error in get(x, envir, mode, inherits) : variable "mylist" 
> was not found
> 
> > lapply(mylist,examplegood,XX=a)
> [[1]]
> [1] 55
> 
> [[2]]
> [1] 110
> 
> [[3]]
> [1] 165
> 
> [[4]]
> [1] 220
> 
> [[5]]
> [1] 275
> 
> Well there you have it!
> 
> Cheers,
> Jason
> 
> 
> On Wed, 11 Feb 2004, Liaw, Andy wrote:
> 
> > My guess is that you probably refer to `mylist' instead of 
> `x' inside
> > `myfun'.  Please show us the entire code, rather than leave 
> us guessing.
> > 
> > Andy
> > 
> > > From: Jason Nielsen
> > > 
> > > Hi all,
> > > 
> > > I'm trying to use lapply on a list with the following command:
> > > 
> > > out<-lapply(mylist,myfun,par1=p,par2=d)               (1)
> > > 
> > > where
> > > 
> > > myfun<-function(x,par1,par1) {.....}                  (2)
> > > 
> > > now this function is in fact a wrapper for some Fortran 
> code I have
> > > written so I think this might be the problem.  When I call 
> > > lapply() as in
> > > (1)  I get the following message:
> > > 
> > > Error in get(x, envir, mode, inherits) : variable "mylist" 
> > > was not found
> > > 
> > > but if I say do:
> > > 
> > > out<-lapply(mylist,sum)
> > > 
> > > it returns a nice list with the sums of the elements in 
> the list.  So
> > > after all that I guess my question is does this have to do 
> > > with the fact
> > > that my function is a wrapper for my Fortran code (which 
> > > works fine on its
> > > own.. and if I use a loop as opposed to lapply() )?  I 
> > > imagine that lapply
> > > which is a wrapper for the .Internal lapply might have some 
> > > trouble with
> > > my Fortran wrapper?  Is this the case or is it something dumb 
> > > on my end?  
> > > Any input is appreciated.
> > > 
> > > Cheers,
> > > Jason
> > > 
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide! 
> > > http://www.R-project.org/posting-guide.html
> > > 
> > > 
> > 
> > 
> > 
> --------------------------------------------------------------
> ----------------
> > Notice:  This e-mail message, together with any 
> attachments, contains information of Merck & Co., Inc. (One 
> Merck Drive, Whitehouse Station, New Jersey, USA 08889), 
> and/or its affiliates (which may be known outside the United 
> States as Merck Frosst, Merck Sharp & Dohme or MSD and in 
> Japan, as Banyu) that may be confidential, proprietary 
> copyrighted and/or legally privileged. It is intended solely 
> for the use of the individual or entity named on this 
> message.  If you are not the intended recipient, and have 
> received this message in error, please notify us immediately 
> by reply e-mail and then delete it from your system.
> > 
> --------------------------------------------------------------
> ----------------
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list