I'm trying to overload an operator, and I'm running into a strange problem. It happens when I install and load the package, but not when I simply source() the code. I'm defining + for two classes. The R code looks like this: #' @export #' @method "+" a `+.a` <- function (x1, x2) { message("Running custom + function") } #' @export #' @method "+" b `+.b` <- `+.a` In some cases I do a+b, and in other cases, I do b+b. I'm told that the +.a and +.b functions must be identical to avoid the error about "Incompatible methods". (In the actual code, the overloaded + function checks the classes of x1 and x2, and then sends them off to other functions.) This is the NAMESPACE file: S3method("+",a) S3method("+",b) I've put the code up at https://github.com/wch/badadd. If I just cut and paste the function definitions to my R session, it works fine: x + y # Running + function # NULL However, if I install and load the package, it gives a warning about incompatible methods, and then seems to fall back to the arithmetic + operator: library(badadd) x + y # [1] 3 # attr(,"class") # [1] "a" # Warning message: # Incompatible methods ("+.a", "+.b") for "+" Is this expected behavior? And if so, is there a workaround? -Winston [[alternative HTML version deleted]]
Hi all, Any ideas about this? As far as I can tell it should work - and I don't understand why it's ok when run outside of a package. Hadley On Wed, Jun 13, 2012 at 7:41 PM, Winston Chang <winstonchang1 at gmail.com> wrote:> I'm trying to overload an operator, and I'm running into a strange problem. > It happens when I install and load the package, but not when I simply > source() the code. > > I'm defining + for two classes. The R code looks like this: > #' @export > #' @method "+" a > `+.a` <- function (x1, x2) { > ? ?message("Running custom + function") > } > > #' @export > #' @method "+" b > `+.b` <- `+.a` > > In some cases I do a+b, and in other cases, I do b+b. I'm told that the +.a > and +.b functions must be identical to avoid the error about "Incompatible > methods". (In the actual code, the overloaded + function checks the classes > of x1 and x2, and then sends them off to other functions.) > > This is the NAMESPACE file: > S3method("+",a) > S3method("+",b) > > I've put the code up at https://github.com/wch/badadd. > > > > If I just cut and paste the function definitions to my R session, it works > fine: > x + y > # Running + function > # NULL > > > However, if I install and load the package, it gives a warning about > incompatible methods, and then seems to fall back to the arithmetic + > operator: > library(badadd) > x + y > # [1] 3 > # attr(,"class") > # [1] "a" > # Warning message: > # Incompatible methods ("+.a", "+.b") for "+" > > > Is this expected behavior? And if so, is there a workaround? > -Winston > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/
Hi, I may be mistaken here but a possible interpretation of: "If a method is found for just one argument or the same method is found for both, it is used." could be that "the same method" here does not mean identical in term of object/content (as identical tests), but means that the two arguments resolve to the same method definition, i.e. a single common method exists for both arguments. This would happen is if a single method is defined for a common parent class. Try: `+.C` <- function(x1, x2){ message("Running custom + function") } x <- structure(1, class=c('A', 'C')) y <- structure(1, class=c('B', 'C')) x + y Another work around would be to define only one method for class 'a' and do the dispatch to `plus.b` in there (might even be able to call it `+.b` if not exported). Might be a good suggestion to add a test in UseMethod to check is the methods are in fact identical. But there might be undesired side effects: two identical methods in different environment that behave differently depending on the state of the environment? By the way any way of seeing any code of primitive function or are these pure C functions, which need to be searched for in R source code? Renaud -- Renaud Gaujoux Computational Biology - University of Cape Town South Africa
Reasonably Related Threads
- List S3 methods and defining packages
- Internally accessing ref class methods with .self$x is different from .self[['x']]
- List S3 methods and defining packages
- How S3method() is implemented and called? And when to use it?
- "Error: package/namespace load failed"