从函数的形式参数访问选择

access choices from formal argument of function

有没有办法访问函数参数的选择?

在这个愚蠢的例子中:

noise <- function(animal = c("dog","cat","chicken","pig")){ # default animal is "dog"
  animal <- match.arg(animal)
  sound <- c("woof","meow","cluck","oink")[match(animal, c("dog","cat","chicken","pig"))]
  sound
}
noise("chicken") # returns desired result

我想通过访问声明中的选择向量来避免重新输入 c("dog","cat","chicken","pig"),类似于(弥补):

noise <- function(animal = c("dog","cat","chicken","pig")){ 
      animal <- match.arg(animal)
      sound <- c("woof","meow","cluck","oink")[match(animal, self.choices("animal"))]
      sound
    }

试试这个:eval(formals(noise)[["animal"]])