简体   繁体   English

在使用函数的包中查找所有函数

[英]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.

相关问题 查找软件包中的所有功能(包括私有功能) - find all functions (including private) in a package 如何在 R package 中找到所有功能? - How to find all functions in an R package? 隐藏包中的未记录函数 - 使用.function_name? - Hiding Undocumented Functions in a Package - Use of .function_name? 使用其他包中的功能 - 何时使用 package::function? - Using functions from other packages - when to use package::function? 我的包功能的修改版本找不到包的其他内部功能 - My modified version of package function can't find the package's other internal functions 为什么我的包函数找不到其他非导出标记函数? - Why doesn't my package function find other non-export tagged functions? 如何在不导入所有函数的情况下在包中使用 data.table? - How can I use data.table in a package without importing all functions? 在 callr::r() 和 callr::r_bg() 中使用加载了 load_all() 的 R package 的函数 - Use functions of an R package loaded with load_all() in callr::r() and callr::r_bg() xml2 包 (R) 中的 xml_find_all 函数未找到相关节点 - xml_find_all function from xml2 package (R) does not find relevant nodes 当函数x是加载包的一部分时,devtools :: load_all()“找不到函数x” - devtools::load_all() “cannot find function x” when function x is part of a loaded package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM