From Matthias.Kohl at uni-bayreuth.de  Sat Jun 26 11:26:28 2004
From: Matthias.Kohl at uni-bayreuth.de (Matthias.Kohl@uni-bayreuth.de)
Date: Sat Jun 26 11:26:32 2004
Subject: [Rd] S4 group "Math", "getGroupMembers", "genericForPrimitive"
Message-ID: <1185.132.180.246.23.1088241988.squirrel@mail.uni-bayreuth.de>

Hi,

I found the following on Windows 2000/NT
R Version 1.9.1  (2004-06-21) (also Version 1.9.0):

The S4 group "Math" doesn't work as documented; i.e., "log", "log10",
"gamma" and "lgamma" are included
in the documentation but don't work. See example code below.

Moreover, what about 'genericForPrimitive' which is used
in 'getGeneric'. It seems that this method is not included in
the R Version 1.9.1 (also 1.9.0). See the example code of
John Chambers at the end of this email.

Why not add the method 'getGroupMembers' as proposed by John Chambers
to the methods package?
(see reply to mail: "Missing 'getGroupMembers()'"
 from Sat May 31 2003 - 15:18:18 EDT)

Thanks for your attention,
Matthias


###################################################
## Example Code
###################################################
## Example Code from the "green book"
setClass("track", representation(x = "numeric", y = "numeric"))

setMethod("Math", "track",
            function(x){ x@y = callGeneric(x@y); x })

tr1 <- new("track", x = 1:3, y = 1:3)
tr1

## are documented as belonging to group "Math"
## see ?"Math"
## but don't work
log(tr1)
log10(tr1)
gamma(tr1)
lgamma(tr1)

## are not generic and don't belong to any group!
is("log", "genericFunction")
is("log10", "genericFunction")
is("gamma", "genericFunction")
is("lgamma", "genericFunction")
getGroup("log")
getGroup("log10")
getGroup("gamma")
getGroup("lgamma")

## make this functions generic and add to group "Math"
## (only local!)
setGeneric("log", function(x, base) standardGeneric("log"), group = "Math")
setGeneric("log10", function(x) standardGeneric("log10"), group = "Math")
setGeneric("gamma", function(x) standardGeneric("gamma"), group = "Math")
setGeneric("lgamma", function(x) standardGeneric("lgamma"), group = "Math")

setMethod("Math", "track",
            function(x){ x@y = callGeneric(x@y); x })

## now works as documented
log(tr1)
log10(tr1)
gamma(tr1)
lgamma(tr1)

## By John Chambers:
## "... the following code implements what one is
## likely to want in most cases." (see reply
## to mail: "Missing 'getGroupMembers()'"
## from Sat May 31 2003 - 15:18:18 EDT)
## Modification of this code
## since 'genericForPrimitive' is not defined (?)
## although it is called in 'getGeneric'!!!
getGroups <- function(what = c(getGenerics(), names(.BasicFunsList))) {
    what <- what[what != "is.function"]
    what <- what[what != "is.null"]
    what <- what[what != "is.object"]
    g <-unlist(sapply(what,
          function(x){
                f <- getGeneric(x)
                if(is(f, "genericFunction"))f@group else NULL
          }))
    split(names(g), g)
}
getGroupMembers <- function(group, whatGenerics) {
    groups <- if(missing(whatGenerics)) getGroups()
              else getGroups(whatGenerics)
    elNamed(groups, group)
}


[Rd] S4 group "Math", "getGroupMembers", "genericForPrimitive"
Matthias.Kohl@uni-bayreuth.de