有没有办法访问函数参数的选择?
在这个愚蠢的例子中:
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
}
答案 0 :(得分:3)
试试这个:eval(formals(noise)[["animal"]])