Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Object methods R

Tags:

r

Given an arbitrary R object, how can I obtain all the methods associated with the object?

like image 299
C. Reed Avatar asked Jan 01 '12 04:01

C. Reed


People also ask

What is UseMethod in R?

(See also the draft 'R Language Definition'.) UseMethod creates a new function call with arguments matched as they came in to the generic. Any local variables defined before the call to UseMethod are retained (unlike S). Any statements after the call to UseMethod will not be evaluated as UseMethod does not return.

How do I find the class of an object in R?

To determine the class of any R object's “internal” type, use the class() function. If the object does not have a class attribute, it has an implicit class, prominently “matrix“, “array“, “function” or “numeric,” or the result of typeof(x). The class() function is robust.

What does class () do in R?

The class() function in R is used to return the values of the class attribute of an R object.

What are S3 and S4 objects in R?

The S3 and S4 software in R are two generations implementing functional object-oriented programming. S3 is the original, simpler for initial programming but less general, less formal and less open to validation. The S4 formal methods and classes provide these features but require more programming.


1 Answers

The closest I can think of is methods (if S3 object/function, List all available methods for an S3 generic function, or all methods for a class.), or showMethods (if S4).

e.g.:

> 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 

Or for a function:

> 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 may also prove a useful read.

like image 52
mathematical.coffee Avatar answered Oct 21 '22 16:10

mathematical.coffee