考虑以下功能:
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