[R] Mapping function dependencies

David Khabie-Zeitoune dave at evocapital.com
Wed Aug 13 15:00:49 CEST 2003


Hi

Does anyone know of any automatic way of mapping out the dependencies of
a function -- i.e. automatically listing which other functions that
function calls? I tried using all.names or all.vars, but I can't get it
to work on a function: e.g.

> test = function(x, y) return(sin(x + y)*cos(x + y))
> all.names(expression(test))
 [1] "test"

I guess the problem is that all.names is not being applied to the body
of the function, whereas if I write the function definition out
explicitly in the expression, I can readily get what I want:

> tmp = all.names(expression(function(x, y) return(sin(x + y)*cos(x +
y))))
> tmp
 [1] "function" "return"   "*"        "sin"      "+"        "x"       
 [7] "y"        "cos"      "+"        "x"        "y"       

and further process this to get the functional dependencies

> pos.fn = sapply(tmp,function(.x) if (exists(.x)) is.function(get(.x))
else FALSE)
> tmp[pos.fn]
 [1] "function" "return"   "*"        "sin"      "+"        "cos"
"+" 

However, this relies on writing out the function definition explicitly
out in the expression which I cannot do as I wish to map the
dependencies of large complex functions.

Thanks for your help.

David




More information about the R-help mailing list