Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I know, I need kepp studying.

Any tips? Thanks in advance.

like image 396
jbkunst Avatar asked Sep 17 '26 22:09

jbkunst


1 Answers

If you just want the parameters, you don't need the entire call. Just use substitute() to access the ... rather than 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(...))))
}
like image 143
MrFlick Avatar answered Sep 20 '26 14:09

MrFlick