[英]Deparse, substitute with three-dots arguments
Let consider a typical deparse(substitute(
R call: 让我们考虑一个典型的deparse(substitute(
R调用:
f1 <-function(u,x,y)
{print(deparse(substitute(x)))}
varU='vu'
varX='vx'
varY='vy'
f1(u=varU,x=varX,y=varY)
That results in 这导致了
[1] "varX"
which is what we expect and we want. 这是我们期望和我们想要的。
Then, comes the trouble, I try to get a similar behaviour using the ...
arguments ie 然后,麻烦,我尝试使用...
参数获得类似的行为即
f2 <- function(...)
{ l <- list(...)
x=l$x
print(deparse(substitute(x))) ### this cannot work but I would like something like that
}
That, not surprisingly, does not work : 毫不奇怪,这不起作用:
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. 我尝试使用不同的解决方案组合来获得预期的行为,但没有一个提供我预期的结果,似乎我仍然不清楚lazy eval在合理的时间内找到自己的方法。
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])
This will give you a named list, similarly to list(...)
, but in its unevaluated form (similarly to what substitute
does on a single argument). 这将为您提供一个命名列表,类似于list(...)
,但是以未评估的形式(类似于单个参数的substitute
)。
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. 另一种方法是使用rlang::quos(...)
如果你愿意使用{rlang}包,它会以稍微不同的形式返回类似的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.