I am creating an S3 class in R for which I would like to be able to do comparisons like "<"
, ">"
, and "=="
. Rather than implement each of these separately from what I've read about group generics I believe I can do so using Ops()
but I haven't found any good examples of how to do this.
Suffice it to say that for myClass
I can create an as.integer.myClass()
function, and that to compare a
and b
I could just convert to integer first:
if(as.integer(a) < as.integer(b)) foo
This totally works, but I would so much rather write
if(a < b) foo
I thought this would work, but it doesn't:
Ops.myClass <- function(e1, e2) {
Ops(as.integer(e1), as.integer(e2))
}
a < bError in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘Ops’ for signature ‘"integer", "integer"’
Any help? Thanks!
Note that
Ops(my, my)
fails with the same error -- you're not invokingOps
, but a generic that is a member of the Ops group. So get the generic and invoke it on the transformed typeswith
The general approach I have for this is to use
.Generic
and switch on the method.