[R] as.character methods

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jan 14 09:38:06 CET 2005


I am moving this thread to R-devel, which is probably where it should have 
started and it is getting even more technical.

On Thu, 13 Jan 2005, McGehee, Robert wrote:

> Professor Ripley,
> You are quite correct, I did have some S3/S4 confusion. Thank you. By
> using an S3 method with the documented usage, I had no trouble writing
> an appropriate as.character method.
>
> I had also not absorbed the fact that S3 and S4 generics might (and do!)
> have different argument lists, as I see that the S3 as.character generic
> takes (x, ...), and the S4 generic takes just (x).
>
> I'd also like to ask a follow-up question to clarify this difference.
> The documentation for ?.BasicFunsList (first page of methods package
> documentation) reads:
>
> "Functions in R that are defined as '.Primitive(<name>)' are not
> suitable for formal methods, because they lack the basic reflectance
> property."
>
> As "as.character"() calls .Primitive("as.character"), I read the above
> sentence to say that S4 methods are not appropriate for this function.
> But certainly S4 methods _can_ be defined for .Primitives, and I can
> seemingly get the argument list for such a primitive with the
> getGeneric() function. Is the point then that S4 cannot pass on extended
> (...) argument list for some primitives (such as the message below), and
> thus informal S3 methods are required in some cases? Or perhaps I'm
> missing the point entirely. Any clarification is greatly appreciated.
>
> Thanks,
> Robert
>
> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Sent: Thursday, January 13, 2005 5:58 PM
> To: McGehee, Robert
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] as.character methods
>
>
> You seem to be confusing `generic' with `S4 generic', and `method' with
> `S4 method'.
>
> Note that all references to `generic' and `method' outside the `methods'
>
> package are not to S4 concepts unless explicitly stated.
>
> On Thu, 13 Jan 2005, McGehee, Robert wrote:
>
>> Hello,
>> ?as.character says that the as.character function is a generic with
>> usage: as.character(x, ...). So, I want to create an S4 object with an
>> as.character method following the above usage, but I get the below
> error
>> telling me that ... isn't in the generic for as.character.
>>
>>> setClass("tmp", "numeric")
>>> setMethod("as.character", "tmp", function(x, ...) paste(x, c(...)))
>> Error in rematchDefinition(definition, fdef, mnames, fnames,
> signature)
>> :
>> 	Methods can add arguments to the generic only if "..." is an
>> argument to the generic
>>
>> Am I reading the documentation incorrectly? How do I correctly pass
> the
>> ... object to the method for this "tmp" object?
>>
>> However I note that looking at the generic function, I see no mention
> of
>> ... (despite the documentation).
>>> getGeneric("as.character")
>> standardGeneric for "as.character" defined from package "base"
>>
>> function (x)
>> standardGeneric("as.character", .Primitive("as.character"))
>> <environment: 0145EDC4>
>> Methods may be defined for arguments: x
>>
>>
>> So, briefly, is the documentation wrong? Am I doing something wrong?
> Can
>> I create an as.character method and pass additional arguments to it as
> I
>> think I should be able to?
>
> Briefly, No, yes, yes.
>
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list