1

I've got a quoted list

quote(list(orders = .N,
           total_quantity = sum(quantity)))

(that I eventually eval in the j part of a data.table)

What I would like is to extract the names of that list without having to evaluate the expression because outside of the correct environment evaluating the expression will produce an error.

RoyalTS
  • 9,545
  • 12
  • 60
  • 101

1 Answers1

5

The list doesn't have any names at that point. It's not even a list. It's a call to the list() function. But that said you can parse that function call and extract name parameter. For example

x <- quote(list(orders = .N,
    total_quantity = sum(quantity)))
names(as.list(x))[-1]
# [1] "orders"         "total_quantity"

That as.list() on the expression turns the function call into a (named) list without evaluation.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • What about just `names(x)[-1]`? Which is weird to me, since `attributes(x)` returns `NULL`. – Rich Scriven May 04 '17 at 01:29
  • Good point @RichScriven. There must be some coercion going on somewhere but I can't find it. The lack of a names attribute is interesting. – MrFlick May 04 '17 at 01:34