[R] lapply and dynamically linked functions

Jason Nielsen jdn at cs.sfu.ca
Thu Feb 12 18:44:36 CET 2004


I figured this much by looking at the function:

> lapply
function (X, FUN, ...) 
{
    FUN <- match.fun(FUN)
    if (!is.list(X)) 
        X <- as.list(X)
    rval <- .Internal(lapply(X, FUN))
    names(rval) <- names(X)
    return(rval)
}
<environment: namespace:base>

however I have to say that the help page doesn't say anything about this
and it was only once I figured out what the problem was that the
realization dawned on me.  I agree that this may not be a bug but it isn't
consistent with the way in which a normal user interacts with the
interpreter, for instance:

> mean<-4           
> dnorm(mean,mean=3)
[1] 0.2419707
> dnorm(4,mean=3)
[1] 0.2419707

ugly I agree and not something you would do intentionally but something
which is done all the time I'm sure... plot(y,x).  I apparently do it
without thinking about it as my little problem shows... X is a design
matrix I'm passing to a little sim (which of course is now XX) I'm doing
so not really that odd of a variable name.  Since in general you don't
have to be concerned with such things I have gotten lazy about variable
names (I realize this is how lapply passes the list to .Internal lapply in
this case).  I guess my point is that maybe a little blurb in the help
page or perhaps a more useful error message than:

Error in get(x, envir, mode, inherits) : variable "mylist"

which isn't very helpful would be appropriate here.

Cheers,
Jason


On Thu, 12 Feb 2004, Liaw, Andy wrote:

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




More information about the R-help mailing list