如何将点参数(省略号)添加到现有函数中

  • 本文关键字:添加 函数 省略号 参数 r
  • 更新时间 :
  • 英文 :


考虑以下功能:

potatoes <- function(n){
    paste(n,"potatoes!")
}
# same body, different formals
potatoes_dots <- function(n,...){
    paste(n,"potatoes!") 
}

我可以称呼什么函数add_dots,以:

identical(add_dots(potatoes)     ,potatoes_dots) # TRUE
identical(add_dots(potatoes_dots),potatoes_dots) # TRUE
add_dots <- function(f) {
  a <- formals(f)
  if (!("..." %in% names(a))) formals(f) <- c(a, alist("..." = ))
  f
}
identical(add_dots(potatoes)     ,potatoes_dots)
#[1] TRUE
identical(add_dots(potatoes_dots),potatoes_dots) 
#[1] TRUE

最新更新