Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deparse, substitute with three-dots arguments

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.

like image 907
user1835313 Avatar asked Mar 06 '19 09:03

user1835313


1 Answers

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.

like image 181
Konrad Rudolph Avatar answered Nov 07 '22 20:11

Konrad Rudolph