Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if object has "as.matrix" method?

Tags:

r

r-s3

r-s4

In my code, I want to check if object d passed to my function has the method as.matrix, i.e. can be converted to matrix. How to do that?

I have tried the following:

"as.matrix" %in% methods(class = class(d))

but it doesn't work, because the methods(class = class(d)) returns a vector of methods, but these are not character strings, see e.g.:

> methods(class = "data.frame")
  [1] $<-                 %&%                 %*%                 [                   [[                  [[<-                [<-                
  [8] aggregate           all.equal           anyDuplicated       anyNA               arrange             as.data.frame       as.list            
 [15] as.matrix           as.vector           as_tibble           barchart            bwplot              by                  cbind              
 [22] cbind2              cloud               coerce              coerce<-            contourplot         count               crossprod

... and I don't know how to convert these to character strings (method names) to perform this test.

EDIT:

PS: After reading the answers and comments, I see this is much more complicated than I originally thought. However, I would still like the answer to the original question, how to find if an object has that method. I am not interested in basic object like vectors in this case, i.e. the as.matrix.default is not an issue here. However, the answer should be able to deal with multiple classes returned by class().

like image 216
Tomas Avatar asked Oct 22 '25 15:10

Tomas


2 Answers

[...] how to convert these to character strings (method names) to perform this test

str() is your friend.

> str(methods(class='data.frame'))
'MethodsFunction' chr [1:67] "[.data.frame" "[[.data.frame" "[[<-,data.frame-method" "[[<-.data.frame" ...
- attr(*, "info")='data.frame':   67 obs. of  4 variables:
 ..$ visible: logi [1:67] TRUE TRUE TRUE TRUE TRUE TRUE ...
 ..$ from   : chr [1:67] "base" "base" "" "base" ...
 ..$ generic: chr [1:67] "[" "[[" "[[<-" "[[<-" ...
 ..$ isS4   : logi [1:67] FALSE FALSE TRUE FALSE TRUE FALSE ...
- attr(*, "byclass")= logi TRUE

We are now able to access generic via

m = attr(methods(class='data.frame'), 'info')$generic
> m
[1] "["             "[["            "[[<-"          "[[<-"          "[<-"           "[<-"          
[7] "$<-"           "$<-"           "aggregate"     "anyDuplicated" "anyNA"         "as.data.frame"
[13] "as.list"       "as.matrix"     "as.vector"     "by"            "cbind"         "coerce"       
[19] "coerce"        "dim"           "dimnames"      "dimnames<-"    "droplevels"    "duplicated"   
[25] "edit"          "format"        "formula"       "getCovariate"  "getGroups"     "getResponse"  
[31] "head"          "initialize"    "is.na"         "Math"          "merge"         "na.exclude"   
[37] "na.omit"       "Ops"           "Ops"           "Ops"           "Ops"           "Ops"          
[43] "plot"          "print"         "prompt"        "rbind"         "row.names"     "row.names<-"  
[49] "rowsum"        "show"          "slotsFromS3"   "sort_by"       "split"         "split<-"      
[55] "stack"         "str"           "subset"        "summary"       "Summary"       "t"            
[61] "tail"          "transform"     "type.convert"  "unique"        "unstack"       "within"       
[67] "xtfrm"        

and

> 'as.matrix' %in% m
[1] TRUE
like image 59
Friede Avatar answered Oct 25 '25 06:10

Friede


Generic idea is to use selectMethod:

 is.function(selectMethod("as.matrix", "data.frame", TRUE))
 [1] TRUE

is.function(selectMethod("as.matrix", class(tibble::tibble(a=1)), TRUE))
[1] TRUE
like image 33
KU99 Avatar answered Oct 25 '25 06:10

KU99