Skip to content

Instantly share code, notes, and snippets.

@MrFlick
Last active December 12, 2015 11:10
Show Gist options
  • Save MrFlick/55ed854eb935e5c21f71 to your computer and use it in GitHub Desktop.
Save MrFlick/55ed854eb935e5c21f71 to your computer and use it in GitHub Desktop.
classMethods.R: list all generic functions for a given class
classMethods <- function(cl) {
if(!is.character(cl)) {
cl<-class(cl)
}
ml<-lapply(cl, function(x) {
sname <- gsub("([.[])", "\\\\\\1", paste0(".", x, "$"))
m <- methods(class=x)
if(length(m)) {
data.frame(
m=as.vector(m),
c=x, n=sub(sname, "", as.vector(m)),
attr(m,"info"),
stringsAsFactors=F
)
} else {
NULL
}
})
df<-do.call(rbind, ml)
df<-df[!duplicated(df$n),]
structure(df$m,
info=data.frame(visible=df$visible, from=df$from),
class="MethodsFunction")
}
g <- glm(y~x,data=data.frame(x=1:10,y=1:10))
classMethods(g)
classMethods(c("glm","lm"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment