【问题标题】:R's ellipsis is ignoring the arguments passedR的省略号忽略了传递的参数
【发布时间】:2019-02-01 18:05:10
【问题描述】:

我写了一个简单的函数,它接受一个默认参数并且还使用了省略号 (...):

myFun <- function(resize = NULL, ...) {
    dots <- list(...)
    str(dots)
}

但是,当我尝试传递与默认参数类似的参数时,无法识别:

> myFun(res = 1)
 list()
> myFun(resi = 2)
 list()

但是,如果我使用不同的名称,它可以正常工作:

> myFun(abc = 2)
List of 1
 $ abc: num 2

> myFun(resize = 3, res = 1)
List of 1
 $ res: num 1

这种行为是有意的吗?如果是这样,是否有适当的方法来处理这个问题(除了重命名参数)?

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1    yaml_2.2.0  

【问题讨论】:

标签: r


【解决方案1】:

这是预期的行为,如 R 语言定义中所述。函数参数部分匹配。

https://cran.r-project.org/doc/manuals/R-lang.html#Arguments

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-01
    • 2019-09-10
    • 2013-06-27
    • 2023-04-03
    • 2019-02-14
    • 2018-06-21
    • 2015-10-07
    • 2023-04-01
    相关资源
    最近更新 更多