给定任意R对象,如何获得与该对象关联的所有方法?

最佳答案

我能想到的最接近的是methods(如果是S3 object / function,List all available methods for an S3 generic function, or all methods for a class.)或showMethods(如果是S4)。

例如。:

> A <- matrix(runif(10))
> B <- methods(class=class(A))
> B
 [1] anyDuplicated.matrix as.data.frame.matrix as.raster.matrix*
 [4] boxplot.matrix       determinant.matrix   duplicated.matrix
 [7] edit.matrix*         head.matrix          isSymmetric.matrix
[10] relist.matrix*       subset.matrix        summary.matrix
[13] tail.matrix          unique.matrix

   Non-visible functions are asterisked
> attr(B,'info')
                     visible                from
anyDuplicated.matrix    TRUE        package:base
as.data.frame.matrix    TRUE        package:base
as.raster.matrix       FALSE registered S3method
boxplot.matrix          TRUE    package:graphics
determinant.matrix      TRUE        package:base
duplicated.matrix       TRUE        package:base
edit.matrix            FALSE registered S3method
head.matrix             TRUE       package:utils
isSymmetric.matrix      TRUE        package:base
relist.matrix          FALSE registered S3method
subset.matrix           TRUE        package:base
summary.matrix          TRUE        package:base
tail.matrix             TRUE       package:utils
unique.matrix           TRUE        package:base

或功能:
> methods(summary)
 [1] summary.aov             summary.aovlist         summary.aspell*
 [4] summary.connection      summary.data.frame      summary.Date
 [7] summary.default         summary.ecdf*           summary.factor
[10] summary.glm             summary.infl            summary.lm
[13] summary.loess*          summary.manova          summary.matrix
[16] summary.mlm             summary.nls*            summary.packageStatus*
[19] summary.PDF_Dictionary* summary.PDF_Stream*     summary.POSIXct
[22] summary.POSIXlt         summary.ppr*            summary.prcomp*
[25] summary.princomp*       summary.srcfile         summary.srcref
[28] summary.stepfun         summary.stl*            summary.table
[31] summary.tukeysmooth*

   Non-visible functions are asterisked
?Methods也可能证明是有用的阅读。

关于r - 获取对象方法R,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8691812/

10-12 19:22