with() and within() functions inside lapply() not seeing outside of its environment?
Hi,
I wouldn't call it a bug, but it's a documented limitation, if you know how to read it. As documented, the expression is evaluated with the caller's environment as the parent environment. But here the caller is some code in lapply, not your function f. x is not found there.
Thanks! That explains it.
I think this modification works, and is maybe the simplest way to get it
to work:
f <- function(x){
y <- list(y1=list())
mywithin <- function(...) within(...)
y <- lapply(y, mywithin, {z<-x})
y
}
The idea here is that the calling frame of f is the environment of
mywithin(), so x is found there.
It works.
Best regards,
Pavel