Choose the Appropriate Method for Ops (original) (raw)
chooseOpsMethod {base} | R Documentation |
---|
Description
chooseOpsMethod
is a function called by the Ops
Group Generic when two suitable methods are found for a given call. It determines which method to use for the operation based on the objects being dispatched.
The function is first called with reverse = FALSE
, wherex
corresponds to the first argument and y
to the second argument of the group generic call. If chooseOpsMethod()
returnsFALSE
for x
, then chooseOpsMethod
is called again, with x
and y
swapped, mx
and my
swapped, and reverse = TRUE
.
Usage
chooseOpsMethod(x, y, mx, my, cl, reverse)
Arguments
x, y | the objects being dispatched on by the group generic. |
---|---|
mx, my | the methods found for objects x and y. |
cl | the call to the group generic. |
reverse | logical value indicating whether x and y are reversed from the way they were supplied to the generic. |
Value
This function must return either TRUE
or FALSE
. A value ofTRUE
indicates that method mx
should be used.
See Also
[Ops](../../base/help/S3groupGeneric.html)
Examples
# Create two objects with custom Ops methods
foo_obj <- structure(1, class = "foo")
bar_obj <- structure(1, class = "bar")
`+.foo` <- function(e1, e2) "foo"
Ops.bar <- function(e1, e2) "bar"
invisible(foo_obj + bar_obj) # Warning: Incompatible methods
chooseOpsMethod.bar <- function(x, y, mx, my, cl, reverse) TRUE
stopifnot(exprs = {
identical(foo_obj + bar_obj, "bar")
identical(bar_obj + foo_obj, "bar")
})
# cleanup
rm(foo_obj, bar_obj, `+.foo`, Ops.bar, chooseOpsMethod.bar)
[Package _base_ version 4.6.0 Index]