Question

I am trying to define the [<- function for my own class following the example in this tutorial http://cran.r-project.org/doc/contrib/Genolini-S4tutorialV0-5en.pdf (page 37)

However, no matter how hard i try, I always get strange error:

setClass(Class = "A",
     representation = representation(x = "numeric"),
     prototype = prototype(x = 0))

setReplaceMethod(f = "[",
             signature = "A",
             definition = function(object, i, j, value) {
                 return(object)
             })

Error in conformMethod(signature, mnames, fnames, f, fdef, definition) : 
  in method for '[<-' with signature 'x="A"': formal arguments (x = "A", i = "A", j = "A", value = "A") 
omitted in the method definition cannot be in the signature

or:

setClass(Class = "A",
     representation = representation(x = "numeric"),
     prototype = prototype(x = 0))

setReplaceMethod(f = "[",
             signature = signature("A", "integer", "integer", "numeric"),
             definition = function(object, i, j, value) {
                 return(object)
             })

Error in conformMethod(signature, mnames, fnames, f, fdef, definition) : 
  in method for '[<-' with signature 'x="A"': formal arguments (x = "A", i = "A", j = "A", value = "A") 
omitted in the method definition cannot be in the signature

Any idea why is this?

Was it helpful?

Solution

In S4 argument names in a definition of an overloaded method have to match the names of the original ones exactly!

setReplaceMethod(f="[", signature="A", function(x, i, j, ..., value){return(x)})

Notice the x (you used object instead).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top