[R-pkg-devel] set generic and methods when slot is a function

Glenn Schultz glennmschultz at me.com
Mon Mar 28 20:09:19 CEST 2016


All,
I am creating a mortgage prepayment model package.  The idea is to create a class FRMModelFunction and each slot in the class is a function that may be use in the prepayment. So I would like to create a generic and method that will allow me to call the slot by name.  However, I think that I am missing something in my setup

If I run the below code I can do the following

Omega(Model) which returns the function

Omega <- Omega(Model) which then assigns the function and allows me to pass values and get a result from Omega(x = 1)

However I can also get a result by directly accessing the slot

Model at Omega(x=1)

What I want to do is set a generic and method so that I can do the following

Omega(x=1)

The below is close but still not what I would like to have.  Is what  I have described possible and if so what have I missed?

Glenn 


setGeneric("Omega", function(object,...) {standardGeneric("Omega")})

 setMethod("Omega", signature = "FRMModelFunction", 
           definition = function(object){object at Omega})

 setClass("FRMModelFunction",
          representation(
            Omega = "function"))

 Model <- new("FRMModelFunction",
           Omega = function(x = numeric()) {return(max(0, min(1, x)))}
           )


More information about the R-package-devel mailing list