[英]find all functions in a package that use a function
I would like to find all functions in a package that use a function. 我想在使用函数的包中找到所有函数。 By functionB "using" functionA I mean that there exists a set of parameters such that functionA is called when functionB is given those parameters.
通过functionB“使用” functionA的意思是,存在一组参数,以便在给functionB那些参数时调用functionA。
Also, it would be nice to be able to control the level at which the results are reported. 同样,能够控制报告结果的水平也是很好的。 For example, if I have the following:
例如,如果我有以下内容:
outer_fn <- function(a,b,c) {
inner_fn <- function(a,b) {
my_arg <- function(a) {
a^2
}
my_arg(a)
}
inner_fn(a,b)
}
I might or might not care to have inner_fn
reported. 我可能会或可能不会在乎是否
inner_fn
报告inner_fn
。 Probably in most cases not, but I think this might be difficult to do. 在大多数情况下可能不是,但是我认为这可能很难做到。
Can someone give me some direction on this? 有人可以给我一些指导吗?
Thanks 谢谢
A small step to find uses of functions is to find where the function name is used. 查找功能用途的一个小步骤是查找功能名称的使用位置。 Here's a small example of how to do that:
这是一个有关如何执行此操作的小示例:
findRefs <- function(pkg, fn) {
ns <- getNamespace(pkg)
found <- vapply(ls(ns, all.names=TRUE), function(n) {
f <- get(n, ns)
is.function(f) && fn %in% all.names(body(f))
}, logical(1))
names(found[found])
}
findRefs('stats', 'lm.fit')
#[1] "add1.lm" "aov" "drop1.lm" "lm" "promax"
...To go further you'd need to analyze the body to ensure it is a function call or the FUN argument to an apply
-like function or the f argument to Map
etc... - so in the general case, it is nearly impossible to find all legal references... ...要走得更远,您需要分析主体以确保它是函数调用或类似于
apply
函数的FUN参数,或者是Map
等的f参数...-所以在一般情况下,它是几乎找不到所有法律参考...
Then you should really also check that getting the name from that function's environment returns the same function you are looking for (it might use a different function with the same name)... This would actually handle your "inner function" case. 然后,您实际上还应该检查从该函数环境中获取的名称是否返回了您要查找的相同函数(它可能使用具有相同名称的其他函数)...这实际上可以处理“内部函数”的情况。
(Upgraded from a comment.) There is a very nice foodweb
function in Mark Bravington's mvbutils package with a lot of this capability, including graphical representations of the resulting call graphs. (从评论中升级。)Mark Bravington的mvbutils软件包中有一个非常不错的
foodweb
函数,具有很多此功能,包括所生成调用图的图形表示。 This blog post gives a brief description. 这篇博客文章给出了简短的描述。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.