[R] Setting Derived Class Slots

Steve Creamer stephen.creamer at nhs.net
Tue Jul 16 15:36:59 CEST 2013


Dear All....I am really struggling with oo in R. Trying to set attributes of
the base class in a derived class method but the slot is only populated in
the method itself, not when I try to print out the object from the console.

Code is  

library(RODBC)
#
# -----------------------------------------------------
# Define a medical event class. This is abstract (VIRTUAL)
# -----------------------------------------------------
#
setClass("Medical_Event",
         representation(
            Event_Name="character",
            Capacity_Profile="numeric",
            Delay_Profile="numeric",
            "VIRTUAL"),
         prototype(Event_Name="An
Event",Capacity_Profile=c(.2,.2,.2,.2,.2,0,0)))

setGeneric("getDelayProfile",function(object){standardGeneric("getDelayProfile")},simpleInheritanceOnly=T)

# ------------------------------------------
# Now define a derived class called GP_Event
# ------------------------------------------

setClass("GP_Event",representation(Surgery_Name="character"),contains=c("Medical_Event"),prototype(Surgery_Name="Unknown"))

# -----------------------------------------
# Now define a derived class called OP_Appt
# -----------------------------------------

setClass("OP_Appt",representation(Clinic_Name="character"),contains=c("Medical_Event"),prototype(Clinic_Name="Unknown"))


setMethod(f="getDelayProfile",signature("OP_Appt"),definition=function(object)
   {
      OpTablesDB<-odbcDriverConnect("DRIVER=Microsoft Access Driver (*.mdb,
*.accdb);
                                     DBQ=Z:\\srp\\Development
Code\\Projects\\CancerPathwaySimulation\\Database\\CancerPathway.accdb")
      strQuery<-"select * from op_profile"
      odbcQuery(OpTablesDB,strQuery)
      dfQuery<-odbcFetchRows(OpTablesDB)
      odbcClose(OpTablesDB)
      delay<-dfQuery$data[[1]][1:70]
      prob<-dfQuery$data[[2]][1:70]
#      as(object,"Medical_Event")@Delay_Profile<-prob
      object at Delay_Profile <- prob
       object
   }
)

if I instantiate a new instance of the derived class 

*aTest<-new("OPP_Appt")*and then try and populate the attribute
Delay_Profile by 

*getDelayProfile(aTest) *

the object slot seems to be populated in the method because I can print it
out, viz

An object of class "OP_Appt"
Slot "Clinic_Name":
[1] "Unknown"

Slot "Event_Name":
[1] "An Event"

Slot "Capacity_Profile":
[1] 0.2 0.2 0.2 0.2 0.2 0.0 0.0

*Slot "Delay_Profile":
 [1]  14  21  25  29  27  49  72  71  43  65 102 134 223 358  24  14  21  25 
35  31  38  43  31  23  21  26  46  54  42  26
[31]  34  24  25  41  48  33  30  17  18  31  24  35  35  24  16  32  36  39 
46  36  26  16  27  21  30  32  33  27   7   5
[61]   9  10   9  11   8   6   1  11  14  10*

but when the method returns and I type

*aTest*

I get 

An object of class "OP_Appt"
Slot "Clinic_Name":
[1] "Unknown"

Slot "Event_Name":
[1] "An Event"

Slot "Capacity_Profile":
[1] 0.2 0.2 0.2 0.2 0.2 0.0 0.0

*Slot "Delay_Profile":
numeric(0)*

ie the Delay_Profile slot is empty????

What haven't I done - can anybody help me please?
Many Thanks

Steve Creamer




--
View this message in context: http://r.789695.n4.nabble.com/Setting-Derived-Class-Slots-tp4671683.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list