在R中的函数之间传递缺少的参数 [英] Passing missing argument from function to function in R

查看:173
本文介绍了在R中的函数之间传递缺少的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,通常的做法是在函数中使用可选参数,然后使用missing()检查它们 (例如,如 SO 22024082 )

I’ve learned that it’s common practice to use optional arguments in function and check them with missing() (e.g. as discussed in SO 22024082)

在此示例中,round0是可选参数(我知道,可以将round0定义为逻辑).

In this example round0 is the optional argument (I know, round0 could be defined as logical).

foo = function(a, round0) {
    a = a * pi
    if(!missing(round0)) round(a)
    else a
}

但是,如果我从另一个函数中调用此函数,该如何传递缺失"呢?

But what if I call this function from another function, how can I pass "missing"?

bar = function(b) {
    if(b > 10) round1=T
    foo(b, round1)
}

如果b< 10,然后未定义bar()中的round1,但无论如何将其传递给foo.如果我修改foo():

If b < 10 then round1 in bar() is not defined, but is passed to foo anyway. If I modify foo():

foo = function(a, round0) {
    a = a * pi
    print(missing(round0))
    print(round0)
    if(!missing(round0)) round(a)
    else a
}

并运行bar(9),输出为:

and run bar(9) the output is:

bar(9)
[1] FALSE
Error in print(round0) : object 'round1' not found
Called from: print(round0)

这意味着:round0不会丢失,但也无法访问?

That means: round0 is not missing, but can’t be accessed either?

我不想在bar()中使用不同的函数调用,如果foo()中有多个可选参数,我将不得不为每个丢失/不丢失的函数编写一个函数调用-所有可选参数的组合.

I don’t want to use different function calls in bar(), if there are several optional arguments in foo(), I would have to write a function call for every missing/not missing - combination of all optional arguments.

是否可以通过遗漏",或者其他解决方案可以解决此问题?

Is it possible to pass "missing", or what other solution would apply for this problem?

推荐答案

在您的示例中,不丢失round0,它设置为round1,未定义(而不是丢失).

In your example, round0 isn't missing, it's set to round1 which is undefined (as opposed to missing).

通常最好的方法是使用默认值,在您的情况下为FALSE:

The best way in general of doing this is to use a default value, in your case FALSE:

foo = function(a, round0 = FALSE) {
    a = a * pi
    if (!round0) round(a)
    else a
}

bar = function(b) {
    round1 <- FALSE
    if (b > 10) round1=TRUE
    foo(b, round1)
}

或无法在参数列表中轻松表示默认值的地方:

or where the default value cannot easily be expressed in the parameter list:

foo = function(a, round0 = NULL) {
    a = a * pi
    if(!is.null(round0)) round(a)
    else a
}

bar = function(b) {
    round1 <- NULL
    if (b > 10) round1=TRUE
    foo(b, round1)
}

请注意,在两种情况下,您都需要在调用函数中将参数手动设置为默认值.

Note in both cases you need to set the parameter to be the default value manually in your calling function.

如果需要,您还可以在if语句中调用带或不带参数的foo函数:

You could also call your foo function with or without an argument if needed within your if statement:

bar = function(b) {
    if (b > 10) foo(b, TRUE) else foo(b)
}

但是,据我所知,除了不能将其作为参数传递外,您不能创建变量missing.

But you can't (as far as I know) make a variable missing other than by just not passing it as an argument.

这篇关于在R中的函数之间传递缺少的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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