0
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

John Paul
  • 12,196
  • 6
  • 55
  • 75
no name
  • 245
  • 2
  • 14
  • You could rewrite function b to take an object of class a, assign the results to slot 4 and 5, then return the object again? – Scott Ritchie Sep 29 '13 at 23:17
  • Classes can be set up to "inherit" but I do not see a sufficiently explained notion of what "merging" would mean to be able to answer this question. Code.... we need code. – IRTFM Sep 29 '13 at 23:24
  • There are about 700 lines of code covering three functions that are passing variables from S4 objects. I have no idea how that software could even be posted here. By merging I mean that I have a superclass that has named slots and also inherits from two other classes. The software runs the other functions and produced results for the other class (not super class). So, I was thinking these classes must be smart enough to simply plug into the right superclass slot. Now, I don't think so it looks like they must be assigned one at a time – no name Sep 30 '13 at 00:12
  • I think you are right Manetheran, but then what is point of having classes inherit from one another. Just set up a big class(?) – no name Sep 30 '13 at 00:14
  • Unless you have situations where it is meaningful to have `class c` by itself, then there is no point having it as a separate parent class. – Scott Ritchie Sep 30 '13 at 00:18
  • class a is a object database (?) that has been serialized out. It holds static data on a fixed income security, coupon, first, next pmt, and maturity, etc.. class c is the results of analysis based on price term structure etc.. so it changes every time. Both need to joined and passed to a function that calculated key rate duration and convexity, the results of which are combined with the previous. So, it is building the analytics for the bond valuation engine. I think I need to conquer these classes. – no name Sep 30 '13 at 00:45
  • then the slots of class c should never be in class a? It seems like a bad idea to store a serialized database for each analysis. You could just pass the analytics results to function b, and a hook to the database A, then create the relevant query? – Scott Ritchie Sep 30 '13 at 00:58

1 Answers1

1

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
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
  • Got it, that is right I have an instance of a then b and would like to create c. thanks -Glenn – no name Sep 30 '13 at 17:17
  • Tried the initialize, still its off a little. I have class a, b, and c. superclass contains a, b, and c. a is stored values, b is a result of a function call using a inputs, and c is the result of a function call using inputs from a and b. I just can't seem to create the superclass. – no name Oct 01 '13 at 01:30
  • Might the use of `?setClassUnion` be useful in this instance? – ctbrown Apr 03 '15 at 11:57
  • @ctbrown Class unions are used when one wishes to create a class that unites two unrelated class hierarchies, whereas here all the classes seem to be in a single hierarchy. Class unions are very weird beasts (to me), and I seldom have use for them. – Martin Morgan Apr 03 '15 at 12:51