R:如何获取函数参数的参数

时间:2017-12-21 11:41:35

标签: r function arguments call

我们说我有一个function需要另外两个functionsargumentsarguments {/ 1}}:

a <- function(x, y = 2){
  x + y
}

b <- function(b1, b2 = 7){
  b1 + b2
}

x <- function(x1, x2){
  # Get arguments of arguments
}

有没有办法从arguments参数中获取x()列表?这是在电话之后:

x(a(3,4), b(5))

我想获得如下列表:

$x1
$x1$x
[1] 3

$x1$y
[1] 4


$x2
$x2$b1
[1] 5

$x2$b2
[1] 7

1 个答案:

答案 0 :(得分:1)

using (var db = new Entities())
{ var q = from u in db.Sites
          where u.SiteURL == result.SiteURL
          select u;
if (q.Any()) {
        List<webpagespeedtest_flat.DataModel.Sites> l = new List<webpagespeedtest_flat.DataModel.Sites>();
        l = q.OrderByDescending(s => s.CheckDate).Take(10).ToList();
        webpagespeedtest_flat.DataModel.Sites[] z = l.ToArray();
int x = z.Count();
for( int i=0; i < z.Count(); i++){
 z[i].Score; // --> this will be the first attribute(x)
 z[i].CheckDate;// -->this will be the second attribute(y)
}

显然,即使使用有效的函数调用,这也很容易被破坏:

x <- function(x1, x2){

  theCall <- lapply(as.list(match.call()),as.list)[-1]


  args <- lapply(theCall, function(x) as.list(formals(as.character(x))))

  Map(function(a, b) {
    b <- b[-1]

    for (i in seq_along(a)) {
      if(i <= length(b)) a[i] <- b[i]
    }
    a
  }, args, theCall)
}

str(x(a(3,4), b(5)))
#List of 2
# $ x1:List of 2
#  ..$ x: num 3
#  ..$ y: num 4
# $ x2:List of 2
#  ..$ b1: num 5
#  ..$ b2: num 7

使所有可能输入的此功能正确,留给读者练习。