Andre Simioni
Andre Simioni

Reputation: 13

Quoting after pipe

I am building a function and would like to retrieve the variable name after a pipe (%>%). Is it possible?
When I use enexpr, it gives the dot sign ("."). My expected answer would be "mpg", "cyl", etc...

> f <- function(x) enexpr(x)
> mtcars$mpg %>% f()
.
> mtcars %>% map(~f(.))
$mpg
.
$cyl
.
$disp
.
$hp
.
$drat
.
$wt
.
$qsec
.
$vs
.
$am
.
$gear
.
$carb
.

Upvotes: 1

Views: 122

Answers (1)

dsz
dsz

Reputation: 5192

If you use imap, you get the name in .y and the main variable in .x (or just .).

e.g.

c(N1='A',N2='B') %>% imap( ~ .y )

Upvotes: 2

Related Questions