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

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

问题描述

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

在这个例子中,round0 是可选参数(我知道,round0 可以定义为逻辑的).

foo = function(a, round0) {a = a * piif(!missing(round0)) round(a)否则一个}

但是如果我从另一个函数调用这个函数,我如何传递missing"呢?

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

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

foo = function(a, round0) {a = a * pi打印(缺少(round0))打印(round0)if(!missing(round0)) round(a)否则一个}

并运行 bar(9) 输出为:

bar(9)[1] 错误打印错误(round0):找不到对象round1"调用自:print(round0)

也就是说:round0没有丢失,但是也不能访问?

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

是否可以通过missing",或者其他什么解决方案可以解决这个问题?

解决方案

在您的示例中,round0 没有丢失,它设置为 round1 未定义(与丢失相反).

通常这样做的最佳方法是使用默认值,在您的情况下FALSE:

foo = function(a, round0 = FALSE) {a = a * pi如果(!round0)round(a)否则一个}酒吧 = 功能(b){第 1 轮 <- 假如果 (b > 10) round1=TRUEfoo(b, round1)}

或者在参数列表中不容易表达默认值的地方:

foo = function(a, round0 = NULL) {a = a * piif(!is.null(round0)) round(a)否则一个}酒吧 = 功能(b){round1 <- NULL如果 (b > 10) round1=TRUEfoo(b, round1)}

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

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

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

@moody_mudskipper 的 answer 展示了一种显示如何生成缺失值的替代方法.

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)

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)
}

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
}

and run bar(9) the output is:

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

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

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?

解决方案

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

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.

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)
}

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

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

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