这是复制“标准”行为的尝试
classMethods <- function(cl) {
if(!is.character(cl)) {
cl<-class(cl)
}
ml<-lapply(cl, function(x) {
sname <- gsub("([.[])", "\\\\\\1", paste0(".", x, "$"))
m <- methods(class=x)
data.frame(
m=as.vector(m),
c=x, n=sub(sname, "", as.vector(m)),
attr(m,"info"),
stringsAsFactors=F
)
})
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)
#or classMethods(c("glm","lm"))
那会回来的
[1] add1.glm* anova.glm confint.glm* cooks.distance.glm*
[5] deviance.glm* drop1.glm* effects.glm* extractAIC.glm*
[9] family.glm* formula.glm* influence.glm* logLik.glm*
[13] model.frame.glm nobs.glm* predict.glm print.glm
[17] residuals.glm rstandard.glm rstudent.glm summary.glm
[21] vcov.glm* weights.glm* alias.lm* case.names.lm*
[25] dfbeta.lm* dfbetas.lm* dummy.coef.lm* hatvalues.lm
[29] kappa.lm labels.lm* model.matrix.lm plot.lm
[33] proj.lm* qr.lm* simulate.lm* variable.names.lm*
Non-visible functions are asterisked
它不像 Josh 那样优雅或简短,但我认为它很好地再现了默认行为。有趣的是,该methods
函数本身大多只是所有已知函数名称的 grep。我从那里借了gsub
东西。