Skip to content
Back to formatted view

Raw Message

Message-ID: <1389313719.14859.88.camel@Navi.krivitsky.homeip.net>
Date: 2014-01-10T00:28:39Z
From: Pavel Krivitsky
Subject: with() and within() functions inside lapply() not seeing outside of its environment?
In-Reply-To: <52CC4528.6020508@gmail.com>

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