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

查看:29
本文介绍了在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)
}

@moody_mudskipper 的答案显示了如何生成缺失值的替代方法.

An alternative approach that shows how to generate a missing value is shown by @moody_mudskipper’s answer.

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

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