Question

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!

Was it helpful?

Solution 2

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

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top