[Rd] Finding inter-function dependencies within a package

Keith Jewell k.jewell at campden.co.uk
Tue Oct 4 12:47:53 CEST 2011

Following helpful correspondence with Mark Bravington, mvbutils::foodweb and 
callers.of can do exactly what I wanted very neatly and easily.

The trick is to use base::asNamespace to see non-exported objects in the 
package. base::asNamespace is described as "Internal name space support 
functions. Not intended to be called directly" so I'll forgive myself for 
not knowing about it previously, and keep an eye open for a more mainstream 
way to do the same job. But given that, this does my job perfectly
> library(mvbutils)
> ff <- foodweb( funs=find.funs( asNamespace( 'sensory')), where= 
> asNamespace( 'sensory'), prune='CreateMeanFizz')

> callers.of('CreateMeanFizz', ff)


where 'sensory' is the loaded package I want to search, and 'CreateMeanFizz' 
is the non-exported function of which I want to find callers.

Very nice!

Thanks Mark!

Keith J

"Keith Jewell" <k.jewell at campden.co.uk> wrote in message 
news:j64058$unj$1 at dough.gmane.org...
> 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