2

I am trying to figure out, why the code below throws the error:

Error in .local(.Object, ...) : argument "data" is missing, with no default

Problem Code:

setClass("A", representation(a="numeric"), "VIRTUAL")
setClass("B", representation(b="numeric"), contains="A")

setMethod("initialize", "A", function(.Object, data){
  .Object@a <- data[1]
})

setMethod("initialize", "B", function(.Object, data){
  .Object@b <- data[2]
  callNextMethod()
})

data <- 1:2

new("B", data)

Thank you for your help!

lucers
  • 21
  • 4

2 Answers2

4

Maybe this is what you are looking for?

setClass("A", representation(a="numeric"), "VIRTUAL")
setClass("B", representation(b="numeric"), contains="A")

setMethod("initialize", "A", function(.Object, data){
  .Object@a <- data[1]
  .Object
})

setMethod("initialize", "B", function(.Object, data){
  .Object@b <- data[2]
  .Object <- callNextMethod(.Object, data)
  .Object
})

data <- 1:2

new("B", data)
  • Thank you, it no longer throws the error, but it also does not initialize the slot "a". `An object of class "B" Slot "b": [1] 2 Slot "a": numeric(0) ` I'm really at a loss to why this does not work. – lucers Aug 11 '13 at 12:23
  • I'm currently looking into this topic, maybe it's the same problem [StackOverflow - Inheritance in R](http://stackoverflow.com/questions/16247583/inheritance-in-r) – lucers Aug 11 '13 at 12:48
0

I found help in this thread: Stack Overflow - Inheritance in R.

See an adapted code example below:

setClass("A", representation(a="numeric"), "VIRTUAL")
setClass("B", representation(b="numeric"), contains="A")

setMethod("initialize", "A", function(.Object,..., a=numeric()){
  .Object@a <- data[1]
  callNextMethod(.Object, ..., a=a)
})

setMethod("initialize", "B", function(.Object,..., b=numeric()){
  .Object@b <- data[2]
  callNextMethod(.Object, ..., b=b)
})

data <- as.numeric(1:2)

new("B",a=data[1],b=data[2])

> An object of class "B"
> Slot "b":
> [1] 2

> Slot "a":
> [1] 1
Community
  • 1
  • 1
lucers
  • 21
  • 4