EB2127
EB2127

Reputation: 1858

S4 error: methods can be defined, but the generic function is implicit, and cannot be changed

I'm creating the following method via S4

#' @name +
#' @title Expand outputs 
#' @description
#'
#' Operator outputs of function 'create' 
#'
#' @return expanded creation
#' @exportMethod +
#' @aliases +
#' @export
setGeneric('+', function(dt, ...) standardGeneric('+'))
setMethod('+', signature(dt = 'data.table'), function(dt, out) {
    return(create(dt, out))
})

I put the following into my R package. When I run devtools::document(), I run into the following problem:

Error in setGeneric("+", function(dt, ...) standardGeneric("+")) : 
  ‘+’ dispatches internally;  methods can be defined, but the generic function is implicit, and cannot be changed.

This appears to be a fatal error, and I cannot create documentation otherwise.

(1) What does this error mean? I'm unsure how I'm supposed to debug this.

(2) When creating an R package, what would be the correct way to work with this error? Should I create the documentation first with another name besides + and then change this afterwards?

Upvotes: 3

Views: 566

Answers (1)

JDL
JDL

Reputation: 1654

As a builtin function, the definition of the generic + cannot be changed, as the error message says. Much of R would break if you did redefine it this way.

+ is defined to have two arguments, e1 and e2. Using this framework, you can modify your example to be

setMethod('+', signature(e1 = 'data.table', e2='ANY'), function(dt, out) {
    return(create(dt, out))
})

One thing to note is that e1 is always the first argument, even if argument names are used. so "+"(e2=A,e1=B) is equal to A+B, not B+A.

Upvotes: 2

Related Questions