[R] getting caller's environment

Yi Zhang yizhang84 at gmail.com
Thu Jan 22 02:03:48 CET 2009


On Wed, Jan 21, 2009 at 5:57 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> Try this:
>
> f <- function(env = parent.frame()) env$x
Thanks. What if the x in "env$x" is an argument passed in? e.g. f <-
function(x, env=parent.frame()) { #assign to env$x ? }
> g <- function(x=1) f()
> x <- 2
> g() # 1
>
> On Wed, Jan 21, 2009 at 5:45 PM, Yi Zhang <yizhang84 at gmail.com> wrote:
>> Hello,
>>
>> I'm writing a function like this:
>>
>> f<-function(x,y,...) {
>> ...
>> assign(x,y,envir=?)
>> }
>>
>> I need the caller (of f) 's environment for the "?" so that the
>> assignment is done at the right place. To be specific, when the code
>> "f(x,1)" appears in environment A, I need the assignment of 1 to x
>> happen in environment A. So my question is how to get the correct
>> environment?
>>
>> Thanks for any suggestions!
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>>
>



-- 
Yi




More information about the R-help mailing list