我有一个R功能:
myFunc <- function(x, base='') {
}
Run Code Online (Sandbox Code Playgroud)
我现在正在扩展该函数,允许一组任意额外的参数:
myFunc <- function(x, base='', ...) {
}
Run Code Online (Sandbox Code Playgroud)
如何禁用参数的部分参数匹配base
?我不能把它放在...
之前,base=''
因为我想保持函数的向后兼容性(它通常被称为myFunction('somevalue', 'someothervalue')
没有base
明确命名).
通过调用我的函数来刺激我:
myFunc(x, b='foo')
Run Code Online (Sandbox Code Playgroud)
我希望这意味着base='', b='foo'
,但R使用部分匹配并假设base='foo'
.
是否有一些代码,我可以插入在myFunc
确定了传递什么参数名,只匹配确切的 "基地"的base
参数,否则在分组它的一部分...
?
这是一个想法:
myFunc <- function(x, .BASE = '', ..., base = .BASE) {
base
}
## Takes fully matching named arguments
myFunc(x = "somevalue", base = "someothervalue")
# [1] "someothervalue"
## Positional matching works
myFunc("somevalue", "someothervalue")
# [1] "someothervalue"
## Partial matching _doesn't_ work, as desired
myFunc("somevalue", b="someothervalue")
# [1] ""
Run Code Online (Sandbox Code Playgroud)