测试多个缺失的参数

时间:2016-02-07 11:25:47

标签: r

假设我有一个包含26个参数的函数(全部是可选的)必须默认为NULL

missingStuff <- function (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o , p,
                          q, r, s, t, u, v, w, x, y, z) {
...
}

我可以测试每个例如

missingStuff <- function (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o , p,
                          q, r, s, t, u, v, w, x, y, z) {

if(missing(a)) a <- NULL
if(missing(b)) b <- NULL
...
}

或者我可以将它们默认为NULL

missingStuff <- function (a = NULL, b = NULL, c = NULL, d = NULL, e = NULL,
                          f = NULL, g = NULL, h = NULL, i = NULL, j = NULL, 
                          k = NULL, l = NULL, m = NULL, n = NULL, o = NULL,
                          p = NULL, q = NULL, r = NULL, s = NULL, t = NULL,
                          u = NULL, v = NULL, w = NULL, x = NULL, y = NULL,
                          z = NULL) {
...
}

但这两个选项乏味而非凌乱。对于我的生活,我找不到合适的方法。当然,这可以使用formals()或许

以更实际的方式完成

这不起作用,无论NULL语句是什么,它都会将所有内容分配给if

missingStuff <- function (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o , p,
                          q, r, s, t, u, v, w, x, y, z) {

  lapply(formals(), function(x) {
    if(missing(x)) x <- NULL
    })
}

也没有

missingStuff <- function (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o , p,
                          q, r, s, t, u, v, w, x, y, z) {

  lapply(formals(), function(x) {
    if(x == "") x <- NULL
    })
}

尽管

formals(missingStuff)[1] == ""
   a 
TRUE

我错过了什么?

3 个答案:

答案 0 :(得分:0)

你可以试试eval

missingStuff <- function (a, b, c, d, e, f)
{
  lapply(names(formals()), function(x)
  { eval(parse(text = paste0("if (missing(",x,")) {assign(\"",x,"\",NULL)}")), envir = parent.env(environment())) } )

  # print them
  for(name in names(formals())) { print(get(name)) }
}

missingStuff(a=1,f=4)

执行时,给出:

> missingStuff(a=1,f=4)
[1] 1
NULL
NULL
NULL
NULL
[1] 4

答案 1 :(得分:0)

你应该使用sys.call ar match.call来提取传递的参数。 然后,您可以设置不同的条件并更新您喜欢的值。

missingStuff <- function (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o , p,
                      q, r, s, t, u, v, w, x, y, z) {


  # get the names of the passed variables
  var <- names(as.list( sys.call() ))[-1]


  # set all the variables that arent passed to NULL
  for(i in 1:length(setdiff(names(formals(missingStuff)),var))){
    assign(setdiff(names(formals(missingStuff)),var)[i],NULL)
  }

  # set all the arguments that are passed as "" or NULL as NULL
  for(i in 1:length(var[mget(var)=="" | is.null(mget(var))])){
    assign(var[mget(var)==""][i],NULL)
  }

  # print all the values of the arguments
  mget(names(formals(missingStuff)))
}

例如运行时:

missingStuff(a=,b=5,c=NULL)

您将获得参数的印刷品:

> missingStuff(a=,b=5,c=NULL)
$a
NULL

$b
[1] 5

$c
NULL

$d
NULL
...

答案 2 :(得分:0)

这是另一种方式。我们过滤形式以保持在调用中找不到参数,因此我们获得了缺少参数的列表,然后将NULL分配给所有参数,然后将列表元素分配给本地环境。

missingStuff <- function (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o , p,
                          q, r, s, t, u, v, w, x, y, z) {
  fmls <- formals()
  missing_fmls <- fmls[!names(fmls) %in% names(match.call()[-1])]
  missing_fmls[] <- list(NULL)
  list2env(missing_fmls, environment())

  print(b)
  print(r)

}
missingStuff()
#> NULL
#> NULL

reprex package(v0.3.0)于2019-10-24创建