Skip to content

`findGlobals()` fails to identify globals in functions produced by a 'factory' pattern

Here the problem is the undefined global variable x

> h = function() { f = function() x; function() f() }
> h()()
Error in f() : object 'x' not found

findGlobals() gets the analysis of h correct, but not the seemingly simpler h()

> codetools::findGlobals(h)
[1] "{" "=" "x"
> codetools::findGlobals(h())
[1] "f"

This can also be seen when findGlobals() is invoked in the defining function

> h = function() { f = function() x; g = function() f(); codetools::findGlobals(g) }
> h()
[1] "f"