From mtmorgan at fhcrc.org  Mon Aug 16 21:12:44 2010
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Mon, 16 Aug 2010 12:12:44 -0700
Subject: [Rd] parent.frame(1) of a S4 method is not a calling
	environment.
In-Reply-To: <87d3tj4ku4.fsf@gmail.com>
References: <87d3tj4ku4.fsf@gmail.com>
Message-ID: <4C698DAC.3090704@fhcrc.org>

On 08/15/2010 02:39 PM, Vitaly S. wrote:
> 
> Dear Developers,
> 
> I wonder what are the parent.frame rules for methods. For ordinary functions one
> can call parent.frame() and be sure that it is the environment of a calling
> function. With S4 aparently it is not the case.
> 
> Here is what I have discovered by trial and error so far:
> 
>> setClass("A", contains="vector")
> [1] "A"
>> setGeneric("foo", def=function(a, ...){standardGeneric("foo")})
> [1] "foo"
>>
>> setMethod("foo", signature("A"), 
> +           def=function(a, ...){
> +             cat("--pf1--\n")
> +             ls(parent.frame(1))
> +             ## cat("--pf2--")
> +             ## ls(parent.frame(2))
> +           })
> [1] "foo"
>>
>> tf <- function(){
> +   b <- 4
> +   foo(new("A")) #ok
> + }
>>
>> tf() #ok
> --pf1--
> [1] "b"
> 
> The above works like predicted.
> Now, a small change. The "b" argument which is not in the signature, but has a
> role of an additional parameter to the function:
> 
>> setMethod("foo", signature("A"), 
> +           def=function(a, b, ...){
> +             cat("--pf1--\n")
> +             print(ls(parent.frame(1)))
> +             cat("--pf2--\n")
> +             print(ls(parent.frame(2)))
> +           })
> [1] "foo"
>>
>> tf()  #oups
> --pf1--
> [1] "a"
> --pf2--
> [1] "b"
>>
> 
> So,  can I be sure that for such functions parent.frame(2) will always work?
> What are the additional rules?

callNextMethod() will cause additional problems; the idea that you'll
grab things from somewhere other than function arguments doesn't seem
like a robust design, even if it's used in some important parts of R.

Martin

> 
> Many thanks,
> Vitaly.
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



[Rd] parent.frame(1) of a S4 method is not a calling environment.
Martin Morgan