Search code examples
rmethodsr6

R, R6, Formals for Public Method


How can I get the formals for a method definition in an R6 class definition?

A = R6Class("MyClass",inherit=NULL,
    public = list(
        fun = function(a,b,c){
            # Do Something
        }
    )
)

So for example, in the above, I would like to get the formals for the fun definition, in the same way one can execute, for example, formals(lm)


Solution

  • You can do this by creating an instance of the class:

    A = R6Class("MyClass",
            inherit=NULL,
            public = list(
                a = NA,
                initialize = function(a){
                    self$a <- a
                },
                fun = function(a,b,c){
                    # Do Something
                }
            )
    )
    B <- A$new(5)
    formals(B$fun)
    

    or by accessing the public methods of the class

    formals(A$public_methods$fun)