A clarification regarding my previous post for those ineterested in the issue:


Looking carefully at Duncan's solution, the key point would be introducing 
envir = environment()

as argument of lapply function (the default envir of do.call function is 
parent.frame() ).

)

 union <- do.call(
               what = rbind,
               args = lapply(
                        X = paste0("dt_sp_", 1:length(sp)),
                        FUN = get,
                       envir = environment()
                       )
 )


________________________________
De: Frank S.
Enviado: viernes, 4 de noviembre de 2016 12:33:11
Para: Duncan Murdoch; r-help@r-project.org; mark.fow...@dfo-mpo.gc.ca
Asunto: Re: [R] When customizing last line, the code stops working


[[elided Hotmail spam]]


As Duncan Murdoch indicated in his answer, it was a matter of envir argument 
from function do.call.


Thank you!


Frank S.

________________________________
De: Duncan Murdoch <murdoch.dun...@gmail.com>
Enviado: jueves, 3 de noviembre de 2016 14:41:26
Para: Frank S.; r-help@r-project.org
Asunto: Re: [R] When customizing last line, the code stops working

On 03/11/2016 8:29 AM, Frank S. wrote:
> Dear all,
>
>
> The function I present works perfectly when I run it as written (that is, 
> leaving NEW LINE as commented). However, when
>
> I try to run the same function via this mentioned line (and therefore 
> commenting LAST LINE) R gives an error message:
> Error in FUN(X[[i]], ...) : object 'dt_sp_1' not found. I do not understand 
> why I don't get the same result.

You aren't specifying the "envir" argument to "get", so it defaults to
the environment from which you called get, and that's the evaluation
frame of lapply() (or maybe of do.call()).

Write the last line as

union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get, envir = 
environment()))


and it works.

Duncan Murdoch
>
>
[[elided Hotmail spam]]
>
>
> Frank S.
>
>
> all.sp <- function(age.u, open, close) {
>
> require(data.table)
> dt <- data.table( id = c(rep(1, 2), 2:4, rep(5, 2)),
>     sex = as.factor(rep(c(0, 1), c(3, 4))),
>     fborn =  as.Date(c("1935-07-25", "1935-07-25", "1939-07-23", "1943-10-05",
>                        "1944-01-01", "1944-09-07", "1944-09-07")) )
>
> sp <- seq(open, close, by = "year")
> dt_sp <- list()
> for (i in 1:length(sp)) {
>    vp <- as.POSIXlt(c(as.Date("1000-01-01"), sp))
>    vp$year <- vp$year - age.u
>    dt.cut <- as.numeric(cut(x = as.POSIXlt(dt$fborn), breaks = vp, right = 
> TRUE, include.lowest = TRUE))
>    dt_sp[i] <- split(dt, factor(dt.cut, i))
>    dt_sp[[i]] <- data.table(dt_sp[[i]])[, entry_sp := sp[i]]
>    assign(paste0("dt_sp_", 1:length(sp))[i], dt_sp[[i]])
>    }
>
>    union <- rbind(dt_sp_1, dt_sp_2, dt_sp_3, dt_sp_4)     # LAST LINE: IT 
> WORKS
>
>
>    # I TRY TO CUSTOMIZE LAST LINE, BUT THEN CODE STOPS WORKING
>    # union <- do.call(rbind, lapply(paste0("dt_sp_", 1:length(sp)), get))     
> # NEW LINE
> }
>
> # Example:
> result <- all.sp(age.u = 65, open = as.Date("2007-01-01"), close = 
> as.Date("2010-05-01"))
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to