Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See which S3 generic methods are available in environment

Sorry if missing something obvious here... Is there any way to see in advance which S3 generic methods currently exist in the current environment (say <environment: R_GlobalEnv>. A list of all current generic methods in base R would do fine but I can't seem to find one.

The reason I'm asking is I'm defining some methods for a class and some of them will already be generic S3 methods so I'd like to know in advance without having to check each one manually.

For example:

isGeneric("mean")
>TRUE

isGeneric("quantile")
>FALSE

At the moment the closest I've come is:

ls(,all.names=TRUE)[sapply(ls(, all.names=TRUE), FUN=isGeneric)]

Which works if I already have a method defined (but doesn't give other potential generic methods) and gives the following error when I give it as a first command in a new R session:

  invalid subscript type 'list'
like image 580
dardisco Avatar asked Apr 04 '13 05:04

dardisco


2 Answers

I doubt that many generic methods will be found within the global environment, they are more likely to be within the environment of a package.

Modifying the example from help ?Filter (which lists all functions in the base package environment) as follows we can filter using isGeneric:

Filter(isGeneric,ls(all.names=TRUE, env = baseenv()))
## [1] "-"             "!="            "$"             "$<-"           "%%"            "%/%"           "&"             "*"            
## [9] "/"             "["             "[["            "[[<-"          "[<-"           "^"             "|"             "+"            
## [17] "<"             "<="            "=="            ">"             ">="            "abs"           "acos"          "acosh"        
## [25] "all"           "any"           "anyDuplicated" "as.character"  "as.data.frame" "as.difftime"   "as.double"     "as.numeric"   
## [33] "as.real"       "asin"          "asinh"         "atan"          "atanh"         "body<-"        "c"             "ceiling"      
## [41] "close"         "cos"           "cosh"          "cummax"        "cummin"        "cumprod"       "cumsum"        "digamma"      
## [49] "dim"           "dim<-"         "duplicated"    "exp"           "expm1"         "floor"         "format"        "gamma"        
## [57] "intersect"     "kronecker"     "length"        "lgamma"        "log"           "log10"         "log1p"         "log2"         
## [65] "max"           "min"           "names"         "print"         "prod"          "range"         "rep"           "rev"          
##  [73] "round"         "setdiff"       "sign"          "signif"        "sin"           "sinh"          "sort"          "sqrt"         
##  [81] "sum"           "summary"       "tan"           "tanh"          "trigamma"      "trunc"         "union"         "unique"

If you need to find what package a function comes from use:

find('function')

In light of your comment: to search all packages on the search path for generic functions, use the following:

Filter(length,sapply(search(), function(x) {
  Filter(isGeneric,ls(all.names=TRUE,env = as.environment(x)))
    } ))

Note that this is wrapped in another Filter statement (to remove elements where length==0).


There is also the internal object .knownS3Generics in the base package environment that will also be useful.

like image 166
mnel Avatar answered Nov 15 '22 00:11

mnel


Here is another method, using utils::isS3stdGeneric in preference to methods::isGeneric.

I used the following to get the functions in the base package:

objs1 <- mget(ls("package:base"), inherits=TRUE)
funs1 <- Filter(is.function, objs)

Now remove those with where the body is null (primitives such as sum) or is a symbol (dontCheck(x), force(x) and identity(x) all simply return x):

genFuns0 <- sapply(
  funs1,
  function(X) {
    if (!is.null(body(X)) & !is.symbol(body(X))) {
      utils::isS3stdGeneric(X)
    }
  },
  simplify=FALSE
)

Then

head(genFuns <- names(Filter(isTRUE, genFuns0)))

gives

[1] "all.equal"     "anyDuplicated" "aperm"         "as.array"     
[5] "as.Date"       "as.expression"

R version 3.6.3 (2020-02-29)

like image 23
Edgar Manukyan Avatar answered Nov 15 '22 01:11

Edgar Manukyan