R-alpha: 'methods(.)' with a 'class' argument

Martin Maechler (maechler@stat.math.ethz.ch)
Mon, 3 Mar 97 12:34:01 +0100


Date: Mon, 3 Mar 97 12:34:01 +0100
Message-Id: <9703031134.AA00400@>
From: Martin Maechler <maechler@stat.math.ethz.ch>
To: R-testers@stat.math.ethz.ch
Subject: R-alpha: 'methods(.)' with a 'class' argument

This is a more useful / extensive  'methods'  function,
modeled after the S one:

methods <- function(generic.function, class)
{
  lis <- unique(c(ls(.SystemEnv), ls(.GlobalEnv)))
  if(!missing(generic.function)) {
    if(!is.character(generic.function))
      generic.function <- deparse(substitute(generic.function))
    name <- paste(generic.function, ".", sep = "")
    lis[substr(lis, 1, nchar(name)) == name]
  } else if(!missing(class)) {
    if(!is.character(class))
      class <- paste(deparse(substitute(class)))
    n.cl <- length(s.cl <- strsplit(class,".")[[1]]) # multiple parts of 'class'
    i0cl <- (1-n.cl):0
    s.lis <- strsplit(lis, ".")
    nparts <- unlist(lapply(s.lis,length))
    has. <- nparts > n.cl   # those that have MORE parts than  class
    r <- NULL
    for(i in which(has.))
      if(all(s.cl == s.lis[[i]][nparts[i]+i0cl]))
	r <- c(r, lis[i])
    r
  }
  else stop("must supply generic.function or class")
}


## Here are few examples of its use:


 cat("methods(print):\n")
print(methods(print))
 cat("methods(class = lm):\n")
print(methods(class = lm)) 

##-- this is harder because 'data.frame'  contains  a ``.'' :
 cat("methods(class = data.frame):\n")
print(methods(class = data.frame))

---
Martin Maechler <maechler@stat.math.ethz.ch>		 <><
Seminar fuer Statistik, SOL F5
ETH (Federal Inst. Technology)	8092 Zurich	 SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1086
http://www.stat.math.ethz.ch/~maechler/
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- For info or help, send "info" or "help",
To [un]subscribe, send "[un]subscribe"
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-