如何弃用参数?

jak*_*kub 5 r

R中是否有弃用参数的标准方法?

示例:对于Web API程序包,我之前包含了一个paging=TRUE参数,它将对所有结果进行分页并下载所有内容。

现在,我想使用一个limit参数,如果设置为,则仅下载所有内容limit=0。这有效地消除了对paging参数的需求,因此,我现在需要让人们现在基本上不执行任何操作。我怎么做?

Joh*_*ith 8

像下面这样的东西也许对你有用?

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 指出的那样,该函数的文档中还应该提到该参数现在已被弃用。

  • 不要对消息使用 `print`。它不能被用户压制。 (2认同)

max*_*low 7

我在寻找重命名包函数的函数参数的解决方案时发现了此讨论。这并不完全是您问题的答案,但非常熟悉,我想也许该解决方案对其他人也有帮助。

因此,为了在不损害现有函数调用的情况下重命名函数的参数,我根据 @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)