chooseOpsMethod {base}R Documentation

Choose the Appropriate Method for Ops


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, where x corresponds to the first argument and y to the second argument of the group generic call. If chooseOpsMethod() returns FALSE for x, then chooseOpsMethod is called again, with x and y swapped, mx and my swapped, and reverse = TRUE.


chooseOpsMethod(x, y, mx, my, cl, reverse)


x, y

the objects being dispatched on by the group generic.

mx, my

the methods found for objects x and y.


the call to the group generic.


logical value indicating whether x and y are reversed from the way they were supplied to the generic.


This function must return either TRUE or FALSE. A value of TRUE indicates that method mx should be used.

See Also



# Create two objects with custom Ops methods
foo_obj <- structure(1, class = "foo")
bar_obj <- structure(1, class = "bar")

`` <- function(e1, e2) "foo" <- function(e1, e2) "bar"

invisible(foo_obj + bar_obj) # Warning: Incompatible methods <- 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, ``,,

[Package base version 4.4.0 Index]