Use match.call to pass all arguments to other function

可紊 提交于 2019-12-05 10:11:59

Regarding your question:

I suppose that the desire to pass all arguments to another function is not very uncommon. Is there a better/standard approach than what I did?

Then I would say this is a more common way to pass the arguments on:

> inner1 <- function(a, b, c) { 
+   return(a + b + c$first + c$second)
+ }
> 
> outer3 <- function(a, b, c) {
+   mycall <- match.call()
+   mycall[[1]] <- as.symbol("inner1") # use inner 1
+   eval(mycall)
+ }
> 
> outer4 <- function(a, b, c) {
+   .args <- as.list(match.call()[-1]) # use inner 1
+   do.call(inner1, .args)
+ }
> 
> outer3(-1, 2, list(first = 3, second = 4))
[1] 8
> outer4(-1, 2, list(first = 3, second = 4))
[1] 8
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!