淡化,用三点参数代替 [英] Deparse, substitute with three-dots arguments

查看:49
本文介绍了淡化,用三点参数代替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑一个典型的 deparse(substitute( R调用:

Let consider a typical deparse(substitute( R call:

f1 <-function(u,x,y)
{print(deparse(substitute(x)))}

varU='vu'
varX='vx'
varY='vy'
f1(u=varU,x=varX,y=varY)

结果

[1] "varX"

这是我们期望和想要的.

which is what we expect and we want.

然后,麻烦了,我尝试使用 ... 自变量(即

Then, comes the trouble, I try to get a similar behaviour using the ... arguments i.e.

f2 <- function(...)
{  l <- list(...)
  x=l$x
  print(deparse(substitute(x))) ### this cannot work but I would like something like that
}

这并不奇怪:

f2(u=varU,x=varX,y=varY)
[1] "\"vx\"" ### wrong ! I would like "varX"

我尝试使用不同的解决方案组合来获得预期的行为,但没有一个能提供预期的结果,而且似乎我对懒惰评估还不够清楚,无法在合理的时间内找到方法.

I tried to get the expected behaviour using a different combination of solutions but none provides me what expected and it seems that I am still not clear enough on lazy eval to find myself the how-to in a resonable amount of time.

推荐答案

您可以通过执行以下操作获取所有未评估参数的列表

You can get the list of all unevaluated arguments by doing

match.call(expand.dots = FALSE)$...

或者,如果您具有点参数,则通过

Or, if you only have dot arguments, via

as.list(match.call()[-1L])

这将为您提供一个命名列表,类似于 list(...),但形式未经评估(类似于 substitute 在单个参数上执行的操作)

This will give you a named list, similarly to list(...), but in its unevaluated form (similarly to what substitute does on a single argument).

如果您愿意使用{rlang}包,则可以使用 rlang :: quos(...),它会以略有不同的形式返回相似的结果.

An alternative is using rlang::quos(...) if you’re willing to use the {rlang} package, which returns a similar result in a slightly different form.

这篇关于淡化,用三点参数代替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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