[R] How '.' is used?

(Ted Harding) Ted.Harding at manchester.ac.uk
Sun Aug 9 22:22:25 CEST 2009


On 09-Aug-09 19:31:47, Duncan Murdoch wrote:
> (Ted Harding) wrote:
> [...]
>> Next -- and this is the real question -- how does R parse the name
>> "summary.glm"? In my naivety, I simply suppose that it looks for
>> an available function whose name is "summary.glm" in just the
>> same way as it looks for "stopifnot", or for that matter "data.matrix"
>> which is not (as far as I know) a compound of a generic function
>> "data" applied to a class "matrix". Then "." would not have a special
>> (parseable) role in the name -- it is simply another "letter".
>>   
> 
> It doesn't do anything special when parsing.  The special sauce comes 
> when the generic summary() function executes UseMethod("summary").
> At that point, we know the class of the object, and we know the name
> of the generic, so it goes looking for a summary.glm method.
> 
> There are some subtleties to how it does that lookup (see the
> discussion in Writing R Extensions about NAMESPACES), but if you had
> a generic function calling UseMethod("data") and it was passed an
> object of class "matrix", data.matrix() would be called, even though
> that doesn't make sense.  This is a flaw in the S3 system, and one of
> the motivations for the development of the S4 system.

Many thanks, Duncan. I think that makes it clear! You've prompted
me to read ?UseMethod':

  When a function calling 'UseMethod("fun")' is applied to an object
  with class attribute 'c("first", "second")', the system searches
  for a function called 'fun.first' and, if it finds it, applies it
  to the object.  If no such function is found a function called
  'fun.second' is tried.  If no class name produces a suitable
  function, the function 'fun.default' is used, if it exists, or an
  error results.

which is pretty explicit that the role of the "." is simply to
construct a name for the system to look for.

One learns ... Thanks.
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 09-Aug-09                                       Time: 21:22:23
------------------------------ XFMail ------------------------------




More information about the R-help mailing list