2014-02-18 48 views
4

有沒有辦法只列出參考類的那些方法,即在類定義中定義的明確(與那些由「系統類」 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" 

回答

3

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([email protected]))) 
+0

謝謝,那也是我當前的解決方法。但是我想知道是否有更多的內置內容,因爲我不喜歡爲了明確比較而創建虛擬類的想法。 – Rappster

+0

我已經添加了第二種方法。 –

+0

哇,一些真棒'過濾器'功夫在那裏! :-)之前從未使用過該功能,但看起來非常強大!謝啦。 – Rappster

2

沒有,因爲在「繼承」從父引用類的方法實際上是在生成時複製到類中。

setRefClass("Example", methods = list(
    a = function() {}, 
    b = function() {} 
)) 

class <- getClass("Example") 
ls([email protected]) 
#> [1] "a"   "b"   "callSuper" "copy"   "export"  
#> [6] "field"  "getClass"  "getRefClass" "import"  "initFields" 
#> [11] "show"   "trace"  "untrace"  "usingMethods" 

但是你可以找到父還明確了方法並返回那些:

parent <- getClass([email protected]) 
ls([email protected]) 
#> [1] "callSuper" "copy"   "export"  "field"  "getClass"  
#> [6] "getRefClass" "import"  "initFields" "show"   "trace"  
#> [11] "untrace"  "usingMethods" 

(請注意,我忽略了你的類有多個家長的可能性,但是這將是易於推廣)

然後用setdiff()找到差異

setdiff(ls([email protected]), ls([email protected])) 
#> [1] "a" "b" 
+0

感謝您澄清。在Gabor更新之前閱讀您的答案,因此認爲他的答案應該「失敗」,因爲Example $ def @ refMethods'看起來很像'class @ refMethods'。但似乎是過濾相應的類名稱('Filter()')的實際屬性條目伎倆! – Rappster