简体   繁体   English

如何在嵌套函数中使用match.call

[英]How use match.call in a nested function

I tried to get the list of names and the expression in ... in a function composition. 我试图让名的列表和表达...在一个函数组成。 Let's suppose a function: 让我们假设一个函数:

g <- function(...) {
  print(as.list(match.call(expand.dots = FALSE))$...)
}

And if we call: 如果我们打电话给:

g(rnorm(5), par = "a", 4 + 4)

We get: 我们得到:

[[1]]
rnorm(5)

$par
[1] "a" 

[[3]]
4 + 4

And it's nice: we can get the expression call for every argument and validate. 很好:我们可以获取每个参数的表达式调用并进行验证。 But I need this but in a function composition : 但是我需要这个但是在函数组成中

f <- function(...)  g(...)

f(rnorm(5), par = "a", 4 + 4)

But I get: 但是我得到:

[[1]]
..1

$par
[1] "a"

[[3]]
..3

I'm reading some chapters http://adv-r.had.co.nz/Expressions.html but I can't find the solution yet. 我正在阅读某些章节http://adv-r.had.co.nz/Expressions.html,但是我找不到解决方案。 I know, I need kepp studying. 我知道,我需要学习。

Any tips? 有小费吗? Thanks in advance. 提前致谢。

If you just want the parameters, you don't need the entire call. 如果只需要参数,则不需要整个调用。 Just use substitute() to access the ... rather than match.call 只需使用substitute()即可访问...而不是match.call

g <- function(...) {
    print(substitute(...()))
}

f <- function(...)  g(...)
f(rnorm(5), par = "a", 4 + 4)

# [[1]]
# rnorm(5)
# 
# $par
# [1] "a"
# 
# [[3]]
# 4 + 4

There's also Hadley's recommendation of 还有哈德利的建议

g <- function(...) {
    print( eval(substitute(alist(...))))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM