List of only explicitly defined methods (Reference Classes)

Is there a way to list only those methods of the reference class that were explicitly defined in the class definition (as opposed to those methods that are inherited by "system classes" such as refObjectGeneratoror envRefClass)?

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

What you are currently getting by calling a method $methods()(see ?setRefClass):

> Example$methods()
 [1] "callSuper"    "copy"         "export"       "field"        "getClass"    
 [6] "getRefClass"  "import"       "initFields"   "show"         "testMethodA" 
[11] "testMethodB"  "trace"        "untrace"      "usingMethods"

What I'm looking for:

> Example$methods()
 [1] "testMethodA" "testMethodB"
+4
source share
2 answers

1) Try the following:

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

2) Here is a second approach that seems to work here, but you can check it out more:

names(Filter(function(x) attr(x, "refClassName") == Example$className, 
    as.list(Example$def@refMethods)))
+3
source

, , "" , .

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

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

, , :

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

( , , , )

setdiff(),

setdiff(ls(class@refMethods), ls(parent@refMethods))
#> [1] "a" "b"
+2

Source: https://habr.com/ru/post/1527410/


All Articles