Get Object methods R

R

R Problem Overview


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

R Solutions


Solution 1 - R

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.

Solution 2 - R

The class of an R object is recovered with class. Objects do not have methods associated with them in typical R parlance. The class of an object determines what function-methods will be applied to it. In order to determine what functions have methods associated with a given class you would need to test all available functions to see whether there was a class-specific method. Even then generic functions would attempt to use a "default" method in most instances.

Some methods associated with a generic S3 function are displayed with methods. The methods of an S4 function are recovered with showMethods. So, for what most people would call "objects", your question does not make sense, but if it happened that you were including functions under the general term "objects" (which is technically fair) then I have answered.

showMethods(classes="data.frame")
methods(class="data.frame")

Then there are a group of methods that might be called "implicit" although their R name is "groupGeneric"

 ?groupGeneric

 methods("Math") # These are "add-on" methods to the primitive Math functions
 [1] Math.data.frame  Math.Date        Math.dates*      Math.difftime    Math.factor     
 [6] Math.mChoice     Math.polynomial* Math.POSIXt      Math.ratetable*  Math.Surv*      
[11] Math.times*     

   Non-visible functions are asterisked
 ?"+"

 methods("Ops")  # The binary operators such as "+", "-", "/"
 [1] Ops.data.frame      Ops.Date            Ops.dates*          Ops.difftime        Ops.factor         
 [6] Ops.findFn          Ops.mChoice         Ops.numeric_version Ops.ordered         Ops.polynomial*    
[11] Ops.POSIXt          Ops.raster*         Ops.ratetable*      Ops.Surv*           Ops.times*         
[16] Ops.ts*             Ops.unit*           Ops.yearmon*        Ops.yearqtr*        Ops.zoo*           

   Non-visible functions are asterisked

And even then you have not really display the members of the Math or the Ops family, but you would have seen them at the help page for ?groupGeneric. You do not see Ops.numeric. A somewhat lower level view is provided by:

 .Primitive("+")
# function (e1, e2)  .Primitive("+")

These will throw an error if offered the wrong class argument.

Solution 3 - R

Some packages define functions that are not methods but which are nevertheless intended for use with a particular class. For example, library(igraph) defines the function radius(_), which is intended for use on objects in the igraph class. Since such functions are not methods, methods(_) and showMethods(_) will not reveal them.

In such cases, lsf.str(_) can be very helpful. For example:

lsf.str("package:igraph")

includes the line:

radius : function (graph, mode = c("all", "out", "in", "total"))  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionC. ReedView Question on Stackoverflow
Solution 1 - Rmathematical.coffeeView Answer on Stackoverflow
Solution 2 - RIRTFMView Answer on Stackoverflow
Solution 3 - RpeakView Answer on Stackoverflow