简体   繁体   English

R的省略号忽略了传递的参数

[英]R's ellipsis is ignoring the arguments passed

I wrote a simple function that takes a default argument and that also makes use of the ellipsis ( ... ): 我编写了一个简单的函数,该函数带有默认参数,并且还使用了省略号( ... ):

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

However, when I try to pass an argument that is similar to the default argument, it is not recognized: 但是,当我尝试传递与默认参数相似的参数时,无法识别该参数:

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

However, if I use a different name it works fine: 但是,如果我使用其他名称,则效果很好:

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

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

Is this behavior intended? 这是故意行为吗? If so, is there a proper way to deal with this issue (besides renaming arguments)? 如果是这样,是否有解决此问题的适当方法(除了重命名参数外)?

> 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  

This is intended behaviour, as stated in the R language definition. 如R语言定义中所述,这是预期的行为。 Function arguments are matched partially. 函数参数部分匹配。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM