Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all functions with methods for a given class

Tags:

r

I'm basically looking for the opposite of methods(some_function) , which returns all class-methods that exist for that function. Is there some easy way to search for all functions which have an explicit method for a given object class?
For example, methods(my_func) returns a pile of myfunc.classname values. Is there a functions(my_class) which would return all functions with a func.my_class method?

like image 939
Carl Witthoft Avatar asked Mar 24 '23 05:03

Carl Witthoft


1 Answers

I think you want to supply an argument to class and nothing to generic.function in methods. Compare

methods(as.matrix) 
[1] as.matrix.data.frame              as.matrix.data.table*             as.matrix.default                
[4] as.matrix.dist*                   as.matrix.noquote                 as.matrix.POSIXlt                
[7] as.matrix.raster*                 as.matrix.SpatialGridDataFrame*   as.matrix.SpatialPixelsDataFrame*

With this, which returns methods for the generic class

methods(class="matrix")
 [1] anyDuplicated.matrix  as.data.frame.matrix  as.data.table.matrix* as.raster.matrix*     boxplot.matrix        corresp.matrix*      
 [7] determinant.matrix    duplicated.matrix     edit.matrix*          head.matrix           isSymmetric.matrix    lda.matrix*          
[13] qda.matrix*           relist.matrix*        subset.matrix         summary.matrix        tail.matrix           unique.matrix        

   Non-visible functions are asterisked

And this also seems to work for S4 classes as well, e.g.

methods(class="data.table")
 [1] $<-.data.table*           [.data.table*             [<-.data.table*           all.equal.data.table*     as.data.frame.data.table*
 [6] as.data.table.data.table* as.list.data.table*       as.matrix.data.table*     dim.data.table*           dimnames.data.table*     
[11] dimnames<-.data.table*    duplicated.data.table*    format.data.table*        head.data.table*          is.na.data.table*        
[16] merge.data.table*         na.omit.data.table*       names<-.data.table*       Ops.data.table*           print.data.table*        
[21] subset.data.table*        tail.data.table*          transform.data.table*     unique.data.table*        within.data.table* 
like image 85
Simon O'Hanlon Avatar answered Apr 24 '23 16:04

Simon O'Hanlon