當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R methods 列出 S3 通用函數或類的方法

R語言 methods 位於 utils 包(package)。

說明

列出 S3 和 S4 通用函數的所有可用方法,或 S3 或 S4 類的所有方法。

用法


   methods(generic.function, class, all.names = FALSE, dropPath = FALSE)
.S3methods(generic.function, class, envir = parent.frame(),
                                    all.names = FALSE, dropPath = FALSE)

## S3 method for class 'MethodsFunction'
format(x, byclass = attr(x, "byclass"), ...)
## S3 method for class 'MethodsFunction'
print(x, byclass = attr(x, "byclass"), ...)

參數

generic.function

泛型函數,或命名泛型函數的字符串。

class

命名類的符號或字符串:僅在未提供 generic.function 時使用。

envir

當泛型函數作為字符串傳遞時,在其中查找泛型函數的定義的環境。

all.names

a logical指示是否返回所有對象名稱。什麽時候FALSE默認情況下,名稱以“開頭”⁠.⁠’被省略。

dropPath

a logical表明如果search()路徑,除了.GlobalEnvpackage:base(IE。,baseenv()),在搜索方法定義時應跳過。默認FALSE向後兼容並且通常需要print()ing,帶或不帶星號;dropPath=TRUE已被硬編碼在R4.3.0 並且對於非小型來說速度更快search()路徑。

x

通常的結果是methods(..), 一個RS3類的對象"MethodsFunction",請參閱下麵的“值”。

byclass

可選的 logical 允許覆蓋 "byclass" 屬性,確定如何打印結果,請參閱“詳細信息”。

...

傳入和傳出方法的潛在進一步參數;目前未使用。

細節

methods() 查找與 generic.functionclass 參數關聯的 S3 和 S4 方法。找到的方法是所有加載的命名空間通過注冊提供的方法,請參閱UseMethod;通常,這包括當前 search() 路徑上的所有包。 .S3methods() 僅查找 S3 方法,.S4methods() 僅查找 S4 方法。

當使用 generic.function 參數調用時,"byclass" 屬性(請參閱詳細信息)為 FALSE ,並且 print 方法默認顯示 S3 和 S4 方法的簽名(全名)。 S3 方法通過將通用函數和類粘貼在一起來打印,並用“.”分隔,如 generic.class 。如果方法定義不是從定義該方法的包命名空間導出的,則 S3 方法名稱後跟星號 *。 S4 方法簽名打印為 generic,class-method ; S4 允許多次調度,因此簽名 generic,A,B-method 中可能有多個類。

當使用 class 參數調用時,"byclass"TRUE ,並且 print 方法默認顯示與類 generic 關聯的泛型函數的名稱。

所有函數的源代碼均可用。對於從命名空間導出的 S3 函數,請在命令行中輸入方法 generic.class 。對於未從命名空間導出的 S3 函數,請參閱 getAnywheregetS3method 。對於 S4 方法,請參閱 getMethod

除了每種通用方法之外,還提供每種方法的幫助。對於交互式幫助,請使用文檔快捷方式 ? 以及通用名稱和製表符完成 ?"generic<tab>" 來選擇需要幫助的方法。

列出的 S3 函數是那些名稱類似於方法的函數,但實際上可能不是方法(代碼中會丟棄已知的異常)。

"MethodsFunction" 的對象,具有 "byclass""info" 屬性的方法名稱的字符向量。 "byclass" 屬性是 logical,指示結果是否是通過定義的參數 class 獲得的。 "info" 屬性是一個包含列的 DataFrame :

通用的

character 泛型名稱向量。

可見的

logical(),方法“visible”是給用戶的嗎?當為 true 時,它通常從定義它的包的命名空間導出,並且包被 attach() 編輯到 search() 路徑。

是S4

logical(),當該方法是 S4 方法時為 true。

a factor ,找到該方法的位置或包名稱。

注意

最初的 methods 函數由 Martin Maechler 編寫。

例子

methods(class = "MethodsFunction") # format and print

require(stats)

methods(summary)
methods(class = "aov")    # S3 class
## The same, with more details and more difficult to read:
print(methods(class = "aov"), byclass=FALSE)
methods("[[")             # uses C-internal dispatching
methods("$")
methods("$<-")            # replacement function
methods("+")              # binary operator
methods("Math")           # group generic
require(graphics)
methods(axis)             # looks like a generic, but is not

mf <- methods(format)     # quite a few; ... the last few :
tail(cbind(meth = format(mf)))

if(require(Matrix, quietly = TRUE)) {
print(methods(class = "Matrix"))  # S4 class
m <- methods(dim)         # S3 and S4 methods
print(m)
print(attr(m, "info"))    # more extensive information

## --> help(showMethods) for related examples
}

參考

Chambers, J. M. (1992) Classes and methods: object-oriented programming in S. Appendix A of Statistical Models in S eds J. M. Chambers and T. J. Hastie, Wadsworth & Brooks/Cole.

也可以看看

S3MethodsclassgetS3method

對於 S4,getMethodshowMethodsIntroductionMethods_Details

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 List Methods for S3 Generic Functions or Classes。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。