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

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

我正在通过 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))
})

我将以下内容放入我的 R 包中。当我运行devtools::document()时,我运行陷入如下问题:

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

这似乎是一个致命错误,否则我无法创建文档。

(1) 这个错误是什么意思?我不确定我应该如何调试它。

(2) 创建 R 包时,处理此错误的正确方法是什么?我是否应该首先使用除 + 之外的其他名称创建文档,然后再更改它?

作为内置函数,泛型+ 的定义不能更改,如错误消息所述。如果您以这种方式重新定义 R 的大部分内容,它就会崩溃。

+ 被定义为有两个参数,e1e2。使用此框架,您可以将示例修改为

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

需要注意的一点是 e1 始终是第一个参数,即使使用了参数名称也是如此。所以 "+"(e2=A,e1=B) 等于 A+B,而不是 B+A.