函数默认参数和命名值 [英] Function default arguments and named values

查看:70
本文介绍了函数默认参数和命名值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个R函数,其中参数可以是几个预定义的命名值之一(其中一个是默认值)或一个自定义字符向量。我应该如何在不依赖魔术价值名称或其他标志的情况下实现这一点?

 #允许使用预定义的子集或传递他们自己的列表
bratPack< -function(members ='CORE',...){
if(members =='CORE')
members< -c('Emilio Estevez','Anthony Michael Hall','Rob Lowe','Andrew McCarthy','Demi Moore','Judd Nelson','Molly Ringwald','Ally Sheedy')
else if(members =='ALL')
成员< -c('Emilio Estevez','Anthony Michael Hall','Rob Lowe','Andrew McCarthy','Demi Moore','Judd Nelson','Molly Ringwald','Ally Sheedy','James Spader ','小罗伯特·唐尼','Jon Cryer','约翰库萨克','凯文培根','雅米格兹','玛丽斯图尔特马斯特森','马修布罗德里克','肖恩佩恩','基弗萨瑟兰' )
...
}


解决方案

<从你的例子中我们可以选择CORE ALL。如果这是两个选项,那么我们在参数'members'的函数定义中指定它们。例如:

  foo < -  function(x,members = c(CORE,ALL)){
##做某事
}

该函数定义为参数设置了允许的值'members',默认为CORE,因为这是第一个命名选项。



在函数体内使用的代码是 match.arg(),正如@Joris已经提到的那样,但是因为我们有如上所述设置函数,我们可以简单地使用 c> match.arg(members)。



我们可以将 foo 写为:

  foo < -  function(x ,members = c(CORE,ALL)){
##评估选项
成员< - match.arg(成员)
##做某事
打印(会员)
}

我们这样使用:

 > foo()
[1]CORE
> foo(members =CORE)
[1]CORE
> foo(membe rs =ALL)
[1]ALL
> foo(members =3rdRate)
match.arg(members)中的错误:'arg'应该是CORE,ALL
中的一个。 $ p>

注意当我们提供一个不包含在选项集中的字符串时的行为。我们得到了一个直观的错误消息,所有这些都是因为我们在函数参数中设置了选项。


Let's say I have an R function in which the arguments can be a one of a few predefined named values (one of which is the default) or a custom character vector. How should I implement this without relying on magic value names or another flag?

#allow use of predefined subsets or pass their own list
bratPack<-function(members='CORE',...){
  if (members=='CORE')
    members<-c('Emilio Estevez','Anthony Michael Hall','Rob Lowe','Andrew McCarthy','Demi Moore','Judd Nelson','Molly Ringwald','Ally Sheedy')
  else if (members=='ALL')
    members<-c('Emilio Estevez','Anthony Michael Hall','Rob Lowe','Andrew McCarthy','Demi Moore','Judd Nelson','Molly Ringwald','Ally Sheedy','James Spader','Robert Downey, Jr.','Jon Cryer', 'John Cusack', 'Kevin Bacon', 'Jami Gertz', 'Mary Stuart Masterson', 'Matthew Broderick', 'Sean Penn', 'Kiefer Sutherland')
  ...
}

解决方案

From your example we have the choice of "CORE" and "ALL". If those are the two options, then we specify them in the function definition for the argument 'members'. E.g.:

foo <- function(x, members = c("CORE", "ALL")) {
    ## do something
}

That function definition sets up the allowed values for argument 'members', with a default of "CORE" as this is the first named option.

The code that one uses within the function body is match.arg(), as @Joris has already mentioned, but because we have set the function up as above, we can simply the usage to just match.arg(members}.

So we can write foo as:

foo <- function(x, members = c("CORE", "ALL")) {
    ## evaluate choices
    members <- match.arg(members)
    ## do something
    print(members)
}

Which we use like this:

> foo()
[1] "CORE"
> foo(members = "CORE")
[1] "CORE"
> foo(members = "ALL")
[1] "ALL"
> foo(members = "3rdRate")
Error in match.arg(members) : 'arg' should be one of "CORE", "ALL"

Notice the behaviour when we supply an string not included in the set of options. We get an intuitive error message, all because we set up the options in the function arguments.

这篇关于函数默认参数和命名值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆