gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:14:38 26 4
gpt4 key购买 nike

我们有一个 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), ...)

对于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, ...)

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

最佳答案

没有“正确”的方法来做到这一点。这是一个偏好问题或一个设计决定,基于您认为函数应该如何处理“额外参数”。例如,有两个变体 A 和 B

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

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

关于r - 在 S3 函数重载中应该把省略号放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54096383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com