Re: [R] lapply not reading arguments from the correct environment

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Fri, 18 May 2007 16:50:56 +0200

subset() was not defined inside myfun(); try this version instead:

myfun <- function () {

    foo <- data.frame(1:10, 10:1)
    foos <- list(foo)
    fooCollumn <- 2
    my.subset <- function(...) subset(...)     cFoo <- lapply(foos, my.subset, select = fooCollumn)     cFoo
}
myfun()

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


> Hello,
>
> I am facing a problem with lapply which I ''''think''' may be a bug.
> This is the most basic function in which I can reproduce it:
>
> myfun <- function()
> {
> foo = data.frame(1:10,10:1)
> foos = list(foo)
> fooCollumn=2
> cFoo = lapply(foos,subset,select=fooCollumn)
> return(cFoo)
> }
>
> I am building a list of dataframes, in each of which I want to keep
> only column 2 (obviously I would not do it this way in real life but
> that's just to demonstrate the bug).
> If I execute the commands inline it works but if I clean my
> environment, then define the function and then execute:
> > myfun()

> I get this error:
> Error in eval(expr, envir, enclos) : object "fooCollumn" not found
> while fooCollumn is defined, in the function, right before lapply.

> In
> addition, if I define it outside the function and then execute the
> function:
> > fooCollumn=1
> > myfun()
> it works but uses the value defined in the general environment and
> not the one defined in the function.
> This is with R 2.5.0 on both OS X and Linux (Fedora Core 6)
> What did I do wrong? Is this indeed a bug? An intended behavior?

> Thanks in advance.
>
> JiHO
> ---
> http://jo.irisson.free.fr/
>
> ______________________________________________
> R-help_at_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
> and provide commented, minimal, self-contained, reproducible code.
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-help_at_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 and provide commented, minimal, self-contained, reproducible code. Received on Fri 18 May 2007 - 15:05:59 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 Fri 18 May 2007 - 15:31:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.