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

Glenn Schultz glennmschultz at me.com
Tue Mar 29 01:27:13 CEST 2016


Kasper,

Thanks! I that was the clue I was looking for.  Simple change to the code makes it work perfectly as you describe.  I also have constructor function for Model beyond the minimal example.  I also like you reference to Bioconductor as that is the model I am trying to follow.

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)))}
            )
  


> On Mar 28, 2016, at 2:14 PM, Kasper Daniel Hansen <kasperdanielhansen at gmail.com> wrote:
> 
> This syntax 
>   Omega(x = 1)
> seems weird.  How do you know that this call should refer back to the Model object and not some other hypothetical Model2 object?  The code you're defining should make something like this work
>   Omega(Model)(x=1)
> where you can see the direct dependence on the Model object.
> 
> (unrelated: One experience from Bioconductor, is that it is good to have explicit constructors for S4 objects instead of using new("...").)
> 
> Best,
> Kasper
> 
> On Mon, Mar 28, 2016 at 2:09 PM, Glenn Schultz <glennmschultz at me.com <mailto:glennmschultz at me.com>> wrote:
> 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)))}
>            )
> ______________________________________________
> R-package-devel at r-project.org <mailto:R-package-devel at r-project.org> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel <https://stat.ethz.ch/mailman/listinfo/r-package-devel>
> 


	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list