[R] with() and within() functions inside lapply() not seeing outside of its environment?
Pavel N. Krivitsky
pavel at uow.edu.au
Tue Jan 7 16:35:03 CET 2014
Hi,
I have a list of sublists, and I want to add and/or remove elements in
each sublist in accordance with a code snippet. I had thought that an
elegant way to do that is using a combination of lapply() and within().
However, the code in the within() call doesn't seem to be able to see
objects outside of it. For (a simplified) example,
f <- function(x){
y <- list(y1=list())
y <- lapply(y, within, {z<-x})
y
}
f(1)
My understanding is that what should happen is that lapply() would
execute
within(y[["y1"]], {z<-x}),
with 1 substituted for x, within() would notice that z has been assigned
1, returning list(z=1), which then gets put into a list as element named
"y1", so the function should ultimately return
list(y1=list(z=1))
What I get instead (on R 3.0.2 and current trunk, both on Linux) is
Error in eval(expr, envir, enclos) : object 'x' not found
Am I doing something wrong, or is this a bug?
Thanks in advance,
Pavel
P.S. If I "hard-code" the value for x, i.e.,
f <- function(){
y <- list(y1=list())
y <- lapply(y, within, {z<-1})
y
}
f()
it returns list(y1=list(z=1)) as expected.
P.P.S. with() has the same problem:
f <- function(x){
y <- list(y1=list())
w <- lapply(y, with, x)
w
}
f(1)
produces the exact same error as within().
More information about the R-help
mailing list