[R] Missing 'getGroupMembers()'

John Chambers jmc at research.bell-labs.com
Sat May 31 15:17:53 CEST 2003


Anton Crombach wrote:
> 
> Hi,
> 
> I'm trying to write a method such that my own classes can be used with the
> groups like "Summary" and "Math", but when I tried to look for examples or
> just wanted to get an idea of which functions are the members of a group, I
> found out that the function "getGroupMembers" is not present... I couldn't
> find an alternative function, if there is one. Does anyone know a solution?
> 

There isn't one currently, and the implementation is not quite as
straightforward in R.

Generic functions in R are objects, with the group they belong to as a
slot.  So, fundamentally, you have to examine some collection of objects
and select those that are generic functions with a particular group
name.

A second complication is that funtions such as "sum" and "+" are not
normally seen as generic functions (so people won't be annoyed by
inefficiency in calling those functions).  To find the groups
corresponding to those functions requires a bit of trickery using
knowledge of how R implements methods for them.

So you have to be a little careful that you are looking in the right set
of generic functions before getGroupMembers is well defined.

Having said all that, the following code implements what one is likely
to want in most cases (the argument recursive= to getGroupMembers is not
supported in this version).  Subject to comments, we can add a version
of it to the package.

------------------------------
getGroups <- function(what = c(getGenerics(), names(.BasicFunsList))) {
    g <-unlist(sapply(what,
          function(x){f <- getGeneric(x);  if(is(f,
"genericFunction"))f at group else NULL}))
    split(names(g), g)
}

getGroupMembers <- function(group, whatGenerics) {
    groups <- if(missing(whatGenerics)) getGroups() else
getGroups(whatGenerics)
    elNamed(groups, group)
}

------------------------------

With this definition, for example,
  
R> getGroupMembers("Math")
 [1] "cumprod" "sqrt"    "abs"     "acos"    "acosh"   "asin"   
"asinh"  
 [8] "atan"    "atanh"   "ceiling" "cos"     "cosh"    "cumsum" 
"exp"    
[15] "floor"   "sin"     "sinh"    "tan"     "tanh"    "trunc"  

Since you mention "Summary", we might as well note now that this group
does not exist in R, because currently the functions that would be its
members are not organized to take methods.

For example,

R> args(max)
function (..., na.rm = FALSE) 
NULL

So there is no first argument on which to attach methods.  If there is a
strong desire, the formal arguments could be changed, to function(x,
..., na.rm = FALSE).  But this needs some discussion (preferrably on
R-devel).

John Chambers

> Anton
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc




More information about the R-help mailing list