如何在不评估R的情况下扩展省略号(...) [英] How to expand an ellipsis (...) argument without evaluating it in R

查看:74
本文介绍了如何在不评估R的情况下扩展省略号(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数,该函数接受任意数量的参数并将其作为表达式存储在变量中,而不对其求值.我设法用match.call做到了,但似乎有点糊涂".

I need a function that accepts an arbitrary number of arguments and stores them in a variable as an expression without evaluating them. I managed to do it with match.call but it seems a little "kludgy".

foo <- function(...) {
  expr <- match.call()
  expr[[1]] <- expression
  expr <- eval(expr)
  # do some stuff with expr
  return(expr)
}

> bla
Error: object 'bla' not found
> foo(x=bla, y=2)
expression(x = bla, y = 2)

澄清

为澄清起见,我在问如何编写行为类似于expression()的函数.出于无法解释的原因,我不能直接使用expression().

Clarification

To clarify, I'm asking how to write a function that behaves like expression(). I can't use expression() directly for reasons that are too long to explain.

推荐答案

最惯用的方法是:

f <- function(x, y, ...) {
  match.call(expand.dots = FALSE)$`...`
}

这篇关于如何在不评估R的情况下扩展省略号(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆