在 S3 函数重载中应该把省略号放在哪里?

Where shall one put the ellipsis in S3 function overloads?

我们有一个 S3 class,我们为其定义了 plot 和其他通用函数。我们不确定 ... 必须去哪里。有两种选择:

  1. plot.hadronacf(x, col = "black", ...)
  2. plot.hadronacf(x, ..., col = "black")

同样适用于 print.summary.

summary的用法中好像不一致:

summary(object, ...)

## Default S3 method:
summary(object, ..., digits)
## S3 method for class 'data.frame'
summary(object, maxsum = 7,
       digits = max(3, getOption("digits")-3), ...)

## S3 method for class 'factor'
summary(object, maxsum = 100, ...)

## S3 method for class 'matrix'
summary(object, ...)

## S3 method for class 'summaryDefault'
format(x, digits = max(3L, getOption("digits") - 3L), ...)
 ## S3 method for class 'summaryDefault'
print(x, digits = max(3L, getOption("digits") - 3L), ...)

对于print,省略号似乎到最后:

print(x, ...)

## S3 method for class 'factor'
print(x, quote = FALSE, max.levels = NULL,
      width = getOption("width"), ...)

## S3 method for class 'table'
print(x, digits = getOption("digits"), quote = FALSE,
      na.print = "", zero.print = "0",
      right = is.numeric(x) || is.complex(x),
      justify = "none", ...)

## S3 method for class 'function'
print(x, useSource = TRUE, ...)

好像最后的省略号用的最多。有这方面的指导方针吗?

没有 "right" 方法可以做到这一点。这是一个偏好问题或设计决定,基于您认为函数应该用 "extra parameters" 做什么。例如,有两个变体 A 和 B

summary(object, maxsum = 100, ...)  # A
summary(object, ..., maxsum = 100)  # B

maxsum 传递给版本 B 的唯一方法是通过函数调用中的命名参数。然而,版本 A 将采用第二个未命名参数并将其传递给 maxsum。它们的不同之处在于 "important" 该参数对函数调用的影响。