gpt4 book ai didi

r - 仅列出显式定义的方法(引用类)

转载 作者:行者123 更新时间:2023-12-05 01:05:10 27 4
gpt4 key购买 nike

有没有办法只列出引用类的那些方法,即 明确 在类定义中定义(与“系统类”继承的那些方法相反,例如 refObjectGeneratorenvRefClass )?

Example <- setRefClass(
Class="Example",
fields=list(
),
methods=list(
testMethodA=function() {
"Test method A"
},
testMethodB=function() {
"Test method B"
}
)
)

您目前通过调用 $methods() 可以获得什么方法(见 ?setRefClass):
> Example$methods()
[1] "callSuper" "copy" "export" "field" "getClass"
[6] "getRefClass" "import" "initFields" "show" "testMethodA"
[11] "testMethodB" "trace" "untrace" "usingMethods"

我在找什么:
> Example$methods()
[1] "testMethodA" "testMethodB"

最佳答案

1) 试试这个:

> Dummy <- setRefClass(Class = "dummy")
> setdiff(Example$methods(), Dummy$methods())
[1] "testMethodA" "testMethodB"

2) 这是第二种方法,它似乎在这里工作,但您可能想要更多地测试它:
names(Filter(function(x) attr(x, "refClassName") == Example$className, 
as.list(Example$def@refMethods)))

关于r - 仅列出显式定义的方法(引用类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21841975/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com