将缺少的参数从函数传递到 R 中的函数

Passing missing argument from function to function in R

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

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

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

但是如果我从另一个函数调用这个函数,我怎么能传递“missing”呢?

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

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

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

和运行 bar(9) 输出是:

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

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

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

是否可以通过 "missing",或者有什么其他解决方案适用于此问题?

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

通常这样做的最佳方法是使用默认值,在您的情况下 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)
}

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

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

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

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

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

@moody_mudskipper 的 .

展示了另一种显示如何生成缺失值的方法

最近遇到了类似的问题,想笼统解决。我认为可以按照下面函数 g() 的定义所示来完成:

f <- function(a = 5, b = 3, c = 2, d = 7) {
  if (missing(a)) {print("a is missing.")}
  if (missing(b)) {print("b is missing.")}
  if (missing(c)) {print("c is missing.")}
  if (missing(d)) {print("d is missing.")}

  cat(a, b, c, d)
}

g <- function(a = 1, b = 1, c = 1, d = 1) {
  args <- as.list(match.call())
  args[[1]] <- NULL # remove first list element, it's the function call
  do.call(f, args, envir = parent.frame())
}

这是我们在使用不同的参数集调用 g() 时得到的结果:

> g()
[1] "a is missing."
[1] "b is missing."
[1] "c is missing."
[1] "d is missing."
5 3 2 7

> g(a = 3)
[1] "b is missing."
[1] "c is missing."
[1] "d is missing."
3 3 2 7

> g(b = 10, c = 10)
[1] "a is missing."
[1] "d is missing."
5 10 10 7

如果您不想将所有参数传递给下一个函数或想要添加一些参数,您可以在 args 列表中添加或删除。作为示例,请参阅以下以一般方式执行此操作的函数 g()

g <- function(a = 1, b = 1, c = 1, x = 1, y = 1, z = 1) {
  f_args <- c("a", "b", "c") # arguments we want to hand off to function f

  # obtain the list of arguments provided
  args <- as.list(match.call())
  # remove first list element, it's the function call
  args[[1]] <- NULL
  # remove the arguments that are not listed in f_args
  args <- args[na.omit(match(f_args, names(args)))]

  # now add argument d, we always want it to be 0:
  args <- c(args, list(d = 0))
  do.call(f, args, envir = parent.frame())
}

这是我们在使用不同的参数集调用 g() 时得到的结果:

> g()
[1] "a is missing."
[1] "b is missing."
[1] "c is missing."
5 3 2 0

> g(a = 3)
[1] "b is missing."
[1] "c is missing."
3 3 2 0

> g(b = 10, c = 10)
[1] "a is missing."
5 10 10 0

有关 do.call() 的更多信息,请参阅

您可以使用不带参数的 substitute() 创建缺少的对象。

在您的情况下,我们可以使 round1 成为 else 子句中缺少的对象:

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

bar = function(b) {
  if(b > 10) round1=T else round1 <- substitute()
  foo(b, round1)
}

bar(9)
#> [1] 28.27433

reprex package (v0.3.0)

于 2019-10-24 创建

rlang 还提供了一个函数 missing_arg() 使参数丢失。

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

bar = function(b) {
  if(b > 10) round1 <- TRUE else round1 <- rlang::missing_arg()
  foo(b, round1)
}

foo(9)
#> [1] 28.27433
bar(9)
#> [1] 28.27433

reprex package (v0.3.0)

创建于 2020-12-02