如何通过“...”父函数中的参数给r中的两个子函数 [英] how to pass the "..." parameters in the parent function to its two children functions in r

查看:73
本文介绍了如何通过“...”父函数中的参数给r中的两个子函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一组参数传递给一个函数。

在下面的SIMPLIFIED示例中,
f_combined 是一个函数,它需要 ...
我想进行下面的函数调用,使得 xx 传递给 f_sqr 并且 yy 传递给 f_plus

$ p $ f_combined(xx = 2,yy = 2)

但它会给我一个错误:

  f_sqr(...)中的错误:未使用的参数(yy = 2)

任何含义? / p>

  f_sqr < -  function(xx = 1){
xx ^ 2
}

f_plus< - function(yy = 1){
yy + 1
}

f_combined< - function(...){
f_sqr(...)+ f_plus(...)
}


解决方案

或者,您可以查询函数的 formals()来查看命名的para它只是期望的米,并且只传递那些米。如果child函数接受 ... ,那么我们将传递所有参数。

  f_combined < -  function(...,.fns = list(f_sqr) (.fns,function(f){
if,f_plus),.reduce =`+`){
dots< - list(...)
Reduce(.reduce,lapply (...%in%names(formals(f))){
do.call(f,dots)
} else if(any(names(dots)%in%names(formals (f))){
do.call(f,dots [names(formals(f))])
} else {
f()
}
}))
}

这里我设置了两个函数作为参数运行( .fns = list(f_sqr,f_plus)),所以可以很容易地更改它们或者可以毫不费力地添加更多。我还设置了一个参数,指定如何组合这些值,如果您想要执行除总和之外的操作( .reduce ='+')。但基本思想是找到最好的一组命名参数并将它们传递给它们。由于你的子函数也有参数的默认值,所以当你传递参数时它们也会运行。



以下是一些示例调用

  f_combined()
#[1] 3
f_combined(xx = 2)
#[1 ] 6
f_combined(yy = 2)
#[1] 4
f_combined(xx = 2,yy = 2)
#[1] 7


I am trying to pass a set of parameters into a function. This function has two sub functions that take part of the above set of parameters.

In the following "SIMPLIFIED" example f_combined is a function that takes ... I would like to make the following function call such that xx is passed to f_sqr and yy is passed to f_plus:

f_combined(xx = 2, yy = 2)

but it would give me an error:

Error in f_sqr(...) : unused argument (yy = 2) 

any suggustions?

f_sqr <- function(xx =1){
  xx ^ 2
}

f_plus <- function(yy =1){
  yy + 1
}

f_combined <- function(...){
  f_sqr(...) + f_plus(...)
}

解决方案

Alternatively you would query a function's formals() to see what named parameters it is expecting and pass along only those. If the "child" function accepts ..., then we will pass along all arguments. This method would not work well for positional arguments.

f_combined <- function(..., .fns =list(f_sqr, f_plus), .reduce=`+`){
   dots <- list(...)
   Reduce(.reduce, lapply(.fns, function(f) {
       if ("..." %in% names(formals(f))) {
           do.call(f, dots)
       } else if ( any( names(dots) %in% names(formals(f)) ) ) {
           do.call(f, dots[names(formals(f))])
       } else {
           f()
       }
   }))
}

Here i've set the two functions to run as a parameter (.fns=list(f_sqr, f_plus)) so they can be easily changed or more can be effortlessly added. I've also set a parameter specifying how to combine those values should you want to do something other than sum them (.reduce='+'). But the basic idea is find the best set of mathching named parameters and pass them along. Since your "child" function have default values for their parameters as well, they will run when you pass along no parameters as well.

Here are some example calls

f_combined()
# [1] 3
f_combined(xx=2)
# [1] 6
f_combined(yy=2)
# [1] 4
f_combined(xx = 2, yy = 2)
# [1] 7

这篇关于如何通过“...”父函数中的参数给r中的两个子函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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