Deparse, substitute with three-dots arguments

天涯浪子 提交于 2021-01-21 09:13:36

问题


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)

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 i.e.

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.


回答1:


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).

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.



来源:https://stackoverflow.com/questions/55019441/deparse-substitute-with-three-dots-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!