4

I am trying to extract out all symbols inside the body of a function which were not defined in that function, but which were defined somewhere up the environment stack. I originally thought that I could do this using the return value of codetools::findGlobals, but apparently this function doesn't do exactly what I thought since it misses variables referenced inside of a quoted expression. See the reproducible example below:

x = 2
y = 1
hidden = function() {
  eval(quote({ x + y }))
}
revealed = function() {
  x + y
}
codetools::findGlobals(hidden)
codetools::findGlobals(revealed)

Given this limitation of findGlobals, is there a preferred alternative technique?

wdkrnls
  • 4,548
  • 7
  • 36
  • 64
  • 1
    This really isn't possible in the general case without actually executing code. I mean if you did something like `eval(as.name(paste0("a","b")))` this would still be hard to detect because you are dynamically building code. But in this case, the function [`find_vars()` from another question](https://stackoverflow.com/a/25374640/2372064) would do what you want and extract all those symbols. – MrFlick Aug 21 '19 at 18:41

0 Answers0