【问题标题】:Extract value of argument from ellipsis without evaluating other arguments从省略号中提取参数的值而不评估其他参数
【发布时间】:2020-11-16 00:03:55
【问题描述】:

在函数中,如何从 省略号,而不评估省略号中的任何其他参数?

具体来说,我将如何修改这个函数的主体以从下面的调用中返回"get me"

foo <- function(...) {
    if (hasArg(bar)) {
        list(...)[["bar"]]
    }
}

foo(bar = paste("get", "me"), baz = oops)
#> Error in foo(bar = paste("get", "me"), baz = oops): object 'oops' not found

【问题讨论】:

    标签: r lazy-evaluation


    【解决方案1】:

    您可以自己捕获调用并明确评估参数

    foo <- function(...) {
      if (hasArg(bar)) {
        eval.parent(match.call()[["bar"]])
      }
    }
    
    foo(bar = paste("get", "me"), baz = oops)
    # [1] "get me"
    

    【讨论】:

      猜你喜欢
      • 2012-11-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多