查看环境中可用的S3通用方法

dar*_*sco 3 generics methods r amazon-s3

对不起,如果遗漏了一些明显的东西...有没有办法提前看到当前环境中当前存在哪些S3泛型方法(比如说<environment: R_GlobalEnv>.基础中所有当前通用方法的列表都R可以,但我似乎无法找到一.

我问的原因是我正在methods为a 定义一些,其中class一些已经是通用的S3方法所以我想事先知道而不必手动检查每一个.

例如:

isGeneric("mean")
>TRUE

isGeneric("quantile")
>FALSE
Run Code Online (Sandbox Code Playgroud)

目前我最接近的是:

ls(,all.names=TRUE)[sapply(ls(, all.names=TRUE), FUN=isGeneric)]
Run Code Online (Sandbox Code Playgroud)

如果我已经定义了一个方法(但没有给出其他可能的通用方法),那么当我在新R会话中将它作为第一个命令提供时,会出现以下错误:

  invalid subscript type 'list'
Run Code Online (Sandbox Code Playgroud)

mne*_*nel 9

我怀疑在全球环境中会发现许多通用方法,它们更有可能在的环境中.

修改help中的示例?Filter(列出基本包环境中的所有函数),如下所示,我们可以使用以下过滤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"
Run Code Online (Sandbox Code Playgroud)

如果您需要找到函数来自哪个包使用:

find('function')
Run Code Online (Sandbox Code Playgroud)

根据您的评论:要搜索通用功能的搜索路径上的所有包,请使用以下命令:

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

请注意,这包含在另一个Filter语句中(删除其中的元素 length==0).


也有内部对象.knownS3Genericsbase封装的环境,也将是有益的.