[R] Creating S4 class with a slot as an array
Morgan, Martin
Martin.Morgan at roswellpark.org
Tue Oct 20 20:44:04 CEST 2015
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Glenn
> Schultz
> Sent: Tuesday, October 20, 2015 10:36 AM
> To: R Help R
> Subject: [R] Creating S4 class with a slot as an array
>
> Hello All,
>
> I am trying to create an S4 class with a slot that is an array. Below is the code I
> tried but I have missed something. Any suggestions are appreciated.
>
> setClass("CashFlow",
> representation(
> CashFlowArray = "array"))
probably here you would also like to provide a prototype
setClass("CashFlow",
representation(CashFlowArray="array"),
prototype=prototype(CashFlowArray=array(numeric(), c(0, 0, 0))))
>
> setMethod("initialize",
> signature = "CashFlow",
> function(.Object,
> CashFlowArray = "array"
> ){
.Object is the prototype, so the next line...
> .Object at CashFlowArray
references the prototypes' default value for the slot, without modifying it, and then...
> return(.Object)
returns the unmodified object, never getting to the following line...
> callNextMethod(.Object,...)
which would have passed the prototype and all arguments that are _not_ CashFlowArray on to the default 'initialize' method.
Maybe you meant
setMethod("initialize", "CashFlow", function(.Object, ..., CashFlowArray=array()) {
callNextMethod(.Object, ..., CashFlowArray=CashFlowArray)
})
or, since the initialize method is now not doing anything, simply not defining it in the first place.
Martin Morgan
> })
> CashFlow <-function(CashFlowArray = array()){ new("CashFlow",
> CashFlowArray = CashFlowArray) }
>
> CFTest <- array(data = 2, c(360,8))
>
> Test <- CashFlow(CashFlowArray = CFTest)
>
> Thanks,
> Glenn
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
This email message may contain legally privileged and/or confidential information. If you are not the intended recipient(s), or the employee or agent responsible for the delivery of this message to the intended recipient(s), you are hereby notified that any disclosure, copying, distribution, or use of this email message is prohibited. If you have received this message in error, please notify the sender immediately by e-mail and delete this email message from your computer. Thank you.
More information about the R-help
mailing list