Question

Example of the problem - class.a has slots 1, 2, 3 
and inherits from class c (4, and 5).  
The slots in class (1, 2, 3) are passed to function.b as variables. 
the output of function b is as class.c.  So now I have the following.



class.a 
   slot 1  a value
   slot 2  a value
   slot 3  a value
   slot 4  empty
   slot 5  empty

   class.c
   slot 4 a result
   slot 5 a result

Can class.c simply be merged into class.a? If so, how? I have searched documentation and I have looked a virtual and superclasses. I cannot find anything on merging classes together

Here is the code that creates the classes -

setClass("BondDetails",
       representation(
       Cusip = "character",
       ID = "character",
       BondType = "character",
       Coupon = "numeric",
       IssueDate = "character",
       DatedDate = "character",
       StartDate = "character",
       Maturity = "character",
       LastPmtDate = "character",
       NextPmtDate = "character",
       Moody = "character",
       SP = "character",
       BondLab  = "character",
       Frequency = "numeric",
       BondBasis = "character",
       Callable = "character",
       Putable = "character",
       SinkingFund = "character"))

setClass("BondCashFlows",
     representation(
     Price = "numeric",
     Acrrued = "numeric",
     YieldToMaturity = "numeric",
     ModDuration = "numeric",
     Convexity = "numeric",
     Period = "numeric",
     PmtDate = "Date",
     TimePeriod = "numeric",
     PrincipalOutstanding = "numeric",  
     CouponPmt = "numeric",
     TotalCashFlow = "numeric"))

 setClass("BondTermStructure",
     representation(
     EffDuration = "numeric",
     EffConvexity = "numeric",
     KeyRateTenor = "numeric",
     KeyRateDuration = "numeric",
     KeyRateConvexity = "numeric"))

 setClass("BondAnalytics",
     contains = c("BondDetails","BondCashFlows", "BondTermStructure"))

BondDetails is stored information BondCashFlows is calculated using inputs from bond details BondTermStructure is calculated using inputs from bond details and bondcashflows

I need to get them all into BondAnalytics so I can create a method of output and I just can't seem to get it right how are these turned into a superclass

Was it helpful?

Solution

Depending on what those 700 other lines of code do... By default the initialize method for S4 classes is a copy constructor, with unnamed arguments taken as instances of base classes. Maybe you mean that you have something like (my classes A, B, C don't correspond to your classes, but somehow the naming seems to make more sense for my understanding of your problem)

.A <- setClass("A", representation(a="numeric"))
.B <- setClass("B", representation(b="numeric"))
.C <- setClass("C", contains=c("A", "B"))

.A(...) is the automatically generated constructor for the "A" class, etc; .A(...) creates a new instance of the class "A" and then calls initialize (i.e., the copy constructor) with the new instance and arguments .... One possibility is that you have an instance of "A" and an instance of "B", and you'd like to construct and instance of "C"

a <- .A(a=1)
b <- .B(b=2)

with

> .C(a, b)              # Construct "C" from base classes "A" and "B"
An object of class "C"
Slot "a":
[1] 1

Slot "b":
[1] 2

Another possibility is that you already have an instance of "C", and you'd like to update the values to contain those from an instance of "B"

b <- .B(b=2)
c <- .C(a=1, b=3)

and then

> initialize(c, b)     # Copy c, replacing slots from 'B' with 'b'
An object of class "C"
Slot "a":
[1] 1

Slot "b":
[1] 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top