简体   繁体   中英

match.call for function with order matched too

I have an R function that accepts both named arguments and others through ... . For example:

f <- function(a, b, ...) {

  mc <- as.list(match.call(expand.dots = TRUE)[-1])

  ### Lots of procesing on mc

  return(mc)

}

So a user might input the call f(a = 3, c = 3, b = 3) , which returns this list:

$a
[1] 3

$b
[1] 3

$c
[1] 3

However, the order of the output does not match the order of the function input arguments. It's clear why that is so, since ... is at the end of the function arguments. But I'm wondering if there is a way to retain the original ordering, even if both named and ... arguments are used. So the output list would look like:

$a
[1] 3

$c
[1] 3

$b
[1] 3

In my research, there is nothing in call , match.call , or formals that allows me to look up the specific input ordering of the call in this case. Any ideas? Many thanks for your help.

You can use sys.call() instead.

f <- function(a, b, ...) {
  mc <- as.list(sys.call()[-1])
  return(mc)

}

f(a = 3, c = 3, b = 3)

# $a
# [1] 3
# 
# $c
# [1] 3
# 
# $b
# [1] 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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