搜索函数源代码 [英] Searching a functions source code
问题描述
在R中,您可以查看函数的来源,因为函数只是另一个对象。我正在寻找一种方法来搜索此源代码,不知道源文件保存在哪里。
例如,我可能想知道函数 shapiro.test
包含函数 sort
(它确实)。
如果 shapiro.test
是一个字符串或一串字符串,我可以使用
grep('sort',shapiro.test)
但是作为 shapiro.test
是一个函数,这会给出错误as.character(x)中的错误:
不能强制类型'关闭'到'字符'类型的向量。
我没有尝试将函数强制转换为字符串。作为一个额外的功能,我并不希望能够在编译时查找基本函数。
解决方案使用 deparse
:
> grep(sort,deparse(shapiro.test))
[1] 5
In R, you can view the source of a function as a function is simply another object.
I am looking for a way to search through this source code, without knowing the file that the source is saved in.
For example, I might want to know if the function shapiro.test
contains the function sort
(it does).
If shapiro.test
was a string or a vector of strings I would use
grep('sort', shapiro.test)
But as shapiro.test
is a function, this gives the error "Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'".
I've had no luck trying to coerce the function to a string. Just as an extra, I'm not expecting to be able to search through base functions as they are compiled.
Here a solution using deparse
:
> grep ("sort", deparse(shapiro.test))
[1] 5
这篇关于搜索函数源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!