3

I would like to list all symbols or names used in a call.

I found the following way but surely there is a more idiomatic or efficient approach ?

expr <- quote(a + b * (a / b))
expr <- as.list(expr)
while(!identical(expr, (expr <- unlist(lapply(expr,as.list))))){}
unique(expr)
#> [[1]]
#> `+`
#> 
#> [[2]]
#> a
#> 
#> [[3]]
#> `*`
#> 
#> [[4]]
#> b
#> 
#> [[5]]
#> `(`
#> 
#> [[6]]
#> `/`

Created on 2019-08-27 by the reprex package (v0.3.0)

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167

1 Answers1

6

You can use all.names to get all symbols used in a call:

expr <- quote(a + b * (a / b))
unique(all.names(expr))
#[1] "+" "a" "*" "b" "(" "/"
GKi
  • 37,245
  • 2
  • 26
  • 48