[Rd] Finding inter-function dependencies within a package

Keith Jewell k.jewell at campden.co.uk
Fri Sep 30 10:54:48 CEST 2011


Thanks for the suggestions. Just to wrap up this thread...

Rainer Krug pointed out that Roxygen did have dependency graphs, although 
Roxygen2 doesn't. But I guess (probably wrongly!) that I'd need to 
process/modify the .R files to use that, and I'm not the package author.

Duncan Murdoch pointed out codetools::findGlobals which can be used to find 
functions called by a target function. But I want to find functions calling 
a target function.

Mark Bravington pointed out mvbutils::foodweb and callers.of which almost do 
what I want (I think it was this I half remembered). But this works in the 
namespace of the package, and my target function isn't exported so foodweb 
doesn't see it!

Working from Duncan's suggestion I came up with this, not pretty or fast, 
could certainly be improved, but it did my one-off job.:
--------------------
# return a character vector of names of functions in 'tarPack' (character) 
which directly call the function 'tarFunc' (character)
called.by <- function(tarFunc, tarPack){
require(codetools)
flist <-   sapply(lsf.str(tarPack, all=TRUE), c)
names(flist) <- NULL
gotit <- sapply(flist, function(x) tarFunc %in% findGlobals(get(x, tarPack), 
FALSE)$functions)
flist[gotit]
}
# e.g.
called.by("CreateMeanFizz", "package:sensory")
------------------------------------------

Thanks again for the input.

Keith Jewell

>> Hi,
>>
>> I'd like to know which functions in a package call one specific
>> function.
>> I think I've seen a tool for identifying such dependencies, but now I
>> can't find it :-( Searches of help and R site search for keywords
>> like function, call, tree, depend haven't helped :-(
>>
>> Can anyone point me in the right direction?
>>
>> Thanks in advance,
>>
>> Keith Jewell
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list