>I am looking for a function that can find the tree of functions called
>in a function that are not S-plus functions.
callTree() does this. The original version was posted by
David Lubinsky to S-news 11/6/91. Here's an updated version.
callTree <- function(fns, where=1, local.only=T) {
# Recursively find functions called by specified functions.
#
# fns - name of a function, or vector of function names
# where - positions in the search list to consider as local functions.
# local.only - only show local functions
#
# This does not find functions called by apply, lapply, eval,
# functions called by methods of generic functions, etc.
# Original by David Lubinsky, revision by Tim Hesterberg
localfns <- c()
for(i in where)
localfns <- c(localfns, objects(where=i, classes="function"))
result <- list()
if(!is.character(fns))
fns <- as.character(substitute(fns))
done <- c()
getFuns <- function(expr) {
if(is.recursive(expr)){
cc <- if(mode(expr) == "call") as.character(expr[[1]])
for(e in expr) cc <- c(cc, Recall(e))
cc
}
else NULL
}
while(length(fns) > 0) {
cf <- fns[1]
cfns <- unique(getFuns(get(cf)))
if(local.only)
cfns <- cfns[!is.na(match(cfns, localfns))]
result[[cf]] <- cfns
done <- c(done, cf)
fns <- c(fns[-1], cfns)
fns <- fns[is.na(match(fns, done)) & !is.na(match(fns, localfns))]
}
result
}
# For `dim(x) <- 3' it returns `dim' rather than `dim<-'
# It includes functions defined internally, if they are also defined externally.
Here is a help file by Alan Zaslavsky, in the old .d format:
.BG
.FN calltree
.TL
Describe dependencies of a function on other functions
.CS
calltree(fns, pos=1, local.only=T)
.AG fns
name of a function or vector of function names
.AG pos
positions in the search list to consider as local functions.
.AG local.only
only show local functions in the calling tree.
.RT
List showing functions called by given functions and so on, recursively.
.EX
> calltree(is.category,2) ### dependencies of a built-in function
$is.category:
[1] ">" "length" "levels"
$">":
[1] ">"
$length:
[1] "length"
$levels:
[1] "levels"
> sort(unique(unlist(calltree(is.category,2)))) ## flat list of dependencies
is.category1 is.category2 is.category3
">" "length" "levels"
.KW ~keyword
.WR
|