简体   繁体   English

R function 中有条件缺少 arguments?

[英]Conditional missing arguments in R function?

I'm trying to work with a function in R that allows for only one of two arguments to be passed, through a series of missing(arg) calls.我正在尝试在 R 中使用 function ,它只允许通过一系列missing(arg)调用传递两个 arguments 中的一个。 I cannot easily modify the function.我不能轻易修改 function。 My data is such that sometimes I supply one argument and other times the other.我的数据是这样的,有时我提供一个论点,而其他时候提供另一个论点。 Is it possible to have some variant of:是否有可能有一些变体:

myFunction(arg1 = switch(condition == T, arg1, NULL), 
           arg2 = switch(condition == F, arg2, NULL))

When I run the function like that, it counts the switch function as an argument and not a NULL object.当我像这样运行 function 时,它会将开关 function 作为参数而不是 NULLB2668CFDE683311DC4Z668CFDE683AC14B56。 Wrapping each switch in eval doesn't help either.将每个switch包装在eval中也无济于事。

Thanks for the help!谢谢您的帮助!

Post script: I know this is bad practice, and I should have the call the function specified by the condition.后记脚本:我知道这是不好的做法,我应该调用条件指定的 function。 But this is part of a magrittr -style pipeline that I'd rather not disturb.但这是我不想打扰的magrittr式管道的一部分。

You're probably much better off using @user2554330 's solution, or, as it seems from comments that you're fine with considering NULL and missing to be equivalent, use missing(arg1) || is.null(arg1)使用 @user2554330 的解决方案可能会好得多,或者,从评论看来,您可以考虑 NULL 和 missing 等效,使用missing(arg1) || is.null(arg1) missing(arg1) || is.null(arg1) in your code. missing(arg1) || is.null(arg1)在您的代码中。

Nevertheless If we really want to type myFunction once only and keep it as is we could do the following:尽管如此,如果我们真的只想输入一次myFunction并保持原样,我们可以执行以下操作:

myFunction <- function(arg1, arg2) {
  if(missing(arg2)) 
    paste(arg1, "apples")
  else 
    paste(arg2, "pears")
}

arg1 <- 2
arg2 <- 3

# SOLUTION 1

# similar to what you've tried, but we build the expression first, then eval
# `substitute()` will become your missing argument here

condition <- TRUE
eval(bquote(myFunction(
  .(if(condition) arg1 else substitute()),
  .(if(!condition) arg2 else substitute())
)))
#> [1] "2 apples"

condition <- FALSE
eval(bquote(myFunction(
  .(if(condition) arg1 else substitute()),
  .(if(!condition) arg2 else substitute())
)))
#> [1] "3 pears"

# SOLUTION 2

# feed to the function the right list of arguments depending on condition

condition <- TRUE
do.call(myFunction, list(arg1 = arg1, arg2 = arg2)[xor(condition,c(FALSE, TRUE))])
#> [1] "2 apples"
condition <- FALSE
do.call(myFunction, list(arg1 = arg1, arg2 = arg2)[xor(condition,c(FALSE, TRUE))])
#> [1] "3 pears"

I think your description is incorrect.我认为你的描述不正确。 With the code you have, one of arg1 or arg2 will be NULL .使用您拥有的代码, arg1arg2之一将是NULL I suspect the function is testing for missing, not testing for NULL .我怀疑 function 正在测试缺失,而不是测试NULL Neither argument is missing.两个论点都没有丢失。

What you could do instead is run你可以做的是运行

if (condition) myFunction(arg1 = arg1) else myFunction(arg2 = arg2)

Regarding keeping it in a pipeline: you can certainly wrap this line in a function that's compatible with magrittr .关于将其保存在管道中:您当然可以将此行包装在与 magrittr 兼容的magrittr中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM