I would like to know if there is any way to override any operator method in R package.
Example for the source in the package:
setclass("clsTest", representation(a="numeric", b="numeric"))
setMethod("+", signature(x1 = "numeric", x2 = "clsTest"),
definition=function(x1, x2) {
Test = x2
Test@a = Test@a+x1
Test@b = Test@b+x1
Test
})
I would like to override the method in the existing package, with
setMethod("+", signature(x1 = "numeric", x2 = "clsTest"),
definition=function(x1, x2) {
Test = x2
Test@a = Test@a+(2*x1)
Test@b = Test@b+(2*x1)
Test
})
I am using R 2.15.2, is there any way to override it?
Your code is very close to correct, but the arguments to +
are called e1
and e2
, not x1
and x2
. With S4, you can't rename them when writing a method.
If you were wondering how you were supposed to know what they were called, you can use args("+")
to check.
The correct code is
setClass("clsTest", representation(a="numeric", b="numeric")) -> clsTest
setMethod("+", signature(e1 = "numeric", e2 = "clsTest"),
definition=function(e1, e2) {
Test = e2
Test@a = Test@a+e1
Test@b = Test@b+e1
Test
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With