简体   繁体   English

match.call也需要匹配顺序的函数

[英]match.call for function with order matched too

I have an R function that accepts both named arguments and others through ... . 我有一个R函数,它通过...接受命名的参数和其他参数。 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: 因此,用户可以输入呼叫f(a = 3, c = 3, b = 3) ,它返回以下列表:

$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. 但是我想知道是否有一种方法可以保留原始顺序,即使同时使用了named和...参数。 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. 在我的研究中,在callmatch.callformals中没有任何内容允许我在这种情况下查找呼叫的特定输入顺序。 Any ideas? 有任何想法吗? Many thanks for your help. 非常感谢您的帮助。

You can use sys.call() instead. 您可以改用sys.call()

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

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

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