Search code examples
rnon-standard-evaluation

get names of quoted list without evaluation


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.


Solution

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