I am experimenting with S4 classes in R and I was trying to define a plus (+
) operator for my objects, i.e. overload the plus operator. I managed to overload the binary +
, but I cannot figure out how to overload the unary plus. Here is a minimal working (unary operator not working) example for what I am trying to achieve:
setClass("HWtest",
representation(expr = "character"),
prototype = list(expr = NA_character_)
)
H <- new("HWtest", expr="Hello")
W <- new("HWtest", expr="World")
setMethod("+", signature(e1="HWtest", e2="HWtest"),
function(e1,e2){
new("HWtest",
expr = paste(e1@expr," + ",e2@expr))
}
)
Now I can use the +
operator and it works smoothly:
H+W
An object of class "HWtest"
Slot "expr":
[1] "Hello + World"
Now the unary plus of course doesn't work, so that has to be overloaded
+H
Error in +H : invalid argument to unary operator
So I tried to overload it in the following way:
setMethod("+", signature(e="HWtest"),
function(e){
new("HWtest",
expr = paste("+ ",e@expr))
}
)
But this generates the error:
Error in match.call(fun, fcall) :
argument 1 matches multiple formal arguments
Is it possible to overload the unary plus? If so, how would I do it for this minimal example?