[R] deparse, substitute and S4 generics

Martin Morgan mtmorgan at fhcrc.org
Wed Jan 23 02:20:47 CET 2008


Michal --

setMethod creates a nested function named .local when method arguments
do not match generic arguments. This changes the environment in which
deparse(substitute()) evaluates, and probably is a significant warning
flag about using these types of idioms with S4.

Creating .local is meant I think for when the method has arguments in
addition to those in the generic, rather than fewer as in your case.

Martin

> showMethods("f", includeDef=TRUE)
Function: f (package .GlobalEnv)
object="ANY"
function (object) 
{
    nam <- deparse(substitute(object))
    cat("name:", nam, "\n")
    object
}



> showMethods("g", includeDef=TRUE)
Function: g (package .GlobalEnv)
object="ANY"
function (object, ...) 
{
    .local <- function (object) 
    {
        nam <- deparse(substitute(object))
        cat("name:", nam, "\n")
        object
    }
    .local(object, ...)
}


Michał Bojanowski <M.J.Bojanowski at uu.nl> writes:

> Hello Bill,
>
> Thanks! Indeed that works. Can you give me a hunch why?
> Does it have anything to do with environments and scope?
>
> ~michal
>
>
>
> Bill.Venables wrote:
>> 
>> Try putting a ... argument in the method for g as well
>> 
>> setGeneric("g", function(object, ...) standardGeneric("g"))
>> 
>> setMethod("g", "ANY",
>> function(object, ...)  ### change
>> {
>>     nam <- deparse(substitute(object))
>>     cat("name:", nam, "\n")
>>     object
>> } ) 
>> 
>> 
>> Now it works.
>> 
>> Bill Venables
>> CSIRO Laboratories
>> PO Box 120, Cleveland, 4163
>> AUSTRALIA
>> Office Phone (email preferred): +61 7 3826 7251
>> Fax (if absolutely necessary):  +61 7 3826 7304
>> Mobile:                         +61 4 8819 4402
>> Home Phone:                     +61 7 3286 7700
>> mailto:Bill.Venables at csiro.au
>> http://www.cmis.csiro.au/bill.venables/ 
>> 
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
>> On Behalf Of Bojanowski, M.J. (Michal)
>> Sent: Wednesday, 23 January 2008 9:48 AM
>> To: r-help at r-project.org
>> Subject: [R] deparse, substitute and S4 generics
>> 
>> Hello everyone,
>> 
>> I encountered the following confusing behavior of 'deparse' and
>> 'substitute' while programming with S4 classes (see example below). It
>> seems like the presence of '...' argument in the definition of the
>> generic generates the problem. I have no clue why, can anyone explain
>> that to me? Are there any "workarounds"?
>> 
>> Thanks a lot for your time!
>> 
>> ~Michal
>> 
>> 
>> 
>> # Let's define two seemingly identical generics
>> 
>> setGeneric("f", function(object) standardGeneric("f"))
>> 
>> setMethod("f", "ANY",
>> function(object)
>> {
>>     nam <- deparse(substitute(object))
>>     cat("name:", nam, "\n")
>>     object
>> } )
>> 
>> # and
>> 
>> setGeneric("g", function(object, ...) standardGeneric("g"))
>> 
>> setMethod("g", "ANY",
>> function(object)
>> {
>>     nam <- deparse(substitute(object))
>>     cat("name:", nam, "\n")
>>     object
>> } )
>> 
>> 
>> 
>> # now let's test:
>> 
>> x <- 1
>> 
>> # this works
>> f(x)
>> 
>> # but this does not,  why? what '...' has to do with it?
>> g(x)
>> 
>> 
>> 
>> ____________________________________
>> Michal Bojanowski
>> ICS / Department of Sociology
>> Utrecht University
>> Heidelberglaan 2; 3584 CS Utrecht
>> The Netherlands
>> m.j.bojanowski at uu dot nl
>> http://www.fss.uu.nl/soc/bojanowski/
>> 
>> 
>> 	[[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> 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.
>> 
>> ______________________________________________
>> 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.
>> 
>> 
>
>
> -----
> ____________________________________
> Michal Bojanowski
> ICS / Department of Sociology
> Utrecht University
> Heidelberglaan 2; 3584 CS Utrecht
> The Netherlands
> m.j.bojanowski at uu dot nl
> http://www.fss.uu.nl/soc/bojanowski/
> http://bojan.3e.pl
> -- 
> View this message in context: http://www.nabble.com/deparse%2C-substitute-and-S4-generics-tp15031691p15031992.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.

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

Location: Arnold Building M2 B169
Phone: (206) 667-2793



More information about the R-help mailing list