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

Mar*_*ing 3 r r-s3

我们有一个S3类,我们为其定义了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), ...)
Run Code Online (Sandbox Code Playgroud)

因为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, ...)
Run Code Online (Sandbox Code Playgroud)

似乎大多数情况下使用了省略号。是否有一些指导方针?

MrF*_*ick 5

没有“正确”的方法可以做到这一点。根据您认为该功能应该对“额外参数”执行的操作,这是偏好问题或设计决策。例如,两个变体A和B

summary(object, maxsum = 100, ...)  # A
summary(object, ..., maxsum = 100)  # B
Run Code Online (Sandbox Code Playgroud)

可以将a传递maxsum给版本B 的唯一方法是通过函数调用中的命名参数。而版本A将采用第二个未命名的参数并将其传递给maxsum。它们在参数对函数调用的“重要性”方面有所不同。