像下面这样的东西也许对你有用?
foo <- function(paging = T, limit = 0) {
if (!missing("paging"))
warning("argument deprecated")
}
Run Code Online (Sandbox Code Playgroud)
示例输出:
# > foo()
# > foo(limit = 0)
# > foo(T)
# Warning message:
# In foo(T) : argument deprecated
# > foo(paging = T)
# Warning message:
# In foo(paging = T) : argument deprecated
Run Code Online (Sandbox Code Playgroud)
正如@Roland 指出的那样,该函数的文档中还应该提到该参数现在已被弃用。
我在寻找重命名包函数的函数参数的解决方案时发现了此讨论。这并不完全是您问题的答案,但非常熟悉,我想也许该解决方案对其他人也有帮助。
因此,为了在不损害现有函数调用的情况下重命名函数的参数,我根据 @John Smith 的答案提出了以下解决方案。
的功能old_arg对于已弃用的函数调用仍然保留foo,而对于新版本的函数调用则被忽略foo。
# old version
foo <- function(x, y, old_arg = c("a", "b", "c")){
old_arg <- match.arg(old_arg, c("a", "b", "c"))
if(old_arg == "a"){
return(x*y)
}else if(old_arg == "b"){
return(x+y)
}else if(old_arg == "c"){
return(x-y)
}
}
# new version
foo <- function(x, y, new_arg = c("a", "b", "c"), old_arg = c("a", "b", "c")){
if (!missing("old_arg")){
warning("Argument deprecated, use new_arg instead.
The parameter new_arg is set equal the parameter old_arg.")
new_arg <- old_arg
}
new_arg <- match.arg(new_arg, c("a", "b", "c"))
if(new_arg == "a"){
return(x*y)
}else if(new_arg == "b"){
return(x+y)
}else if(new_arg == "c"){
return(x-y)
}
}
Run Code Online (Sandbox Code Playgroud)