有没有一种方法可以获取包含 R 中可以使用的所有函数名称的向量?
我想要一个调用返回一个向量,其中包含我可以在当前 R 会话中调用的所有函数的名称。有人知道如何实现这一目标吗?
(我想根据此向量检查用户输入的变量。用户输入例如 c
作为变量名称时,我们遇到了一些不可预见的问题)
更新: 我想获得当前加载的所有包中的函数名称。
解决方案(半途):基于 Joris Meys 的 lsf.str()
提示,我想出了以下函数,该函数返回包含所有当前可用函数名称的排序向量:
getFunctionNames <- function() {
loaded <- (.packages())
loaded <- paste("package:", loaded, sep ="")
return(sort(unlist(lapply(loaded, lsf.str))))
}
但也请参阅 Joris Meys 帖子的评论以获得更好的答案。
I would like to have a call that returns me a vector with the names of all function that I could call in the current R session. Does anybody know how to achieve this?
(I would like to check user entered variables against this vector. We had some unforseen problem with users entering e.g., c
as variable names)
UPDATE: I would like to get the function names from all packages currently loaded.
SOLUTION (half way): Based on Joris Meys tip with lsf.str()
I came up with the following function that returns a sorted vector with all currently available function names:
getFunctionNames <- function() {
loaded <- (.packages())
loaded <- paste("package:", loaded, sep ="")
return(sort(unlist(lapply(loaded, lsf.str))))
}
Bu,t see also the comments on Joris Meys' post for even better answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用 lsf.str() 作为开始。
例如:
x <- as.character(lsf.str("package:base"))
为您提供基础包中所有函数的列表。您可以添加所有要检查的包。首先想到的是stats
和utils
。编辑:关于您有关当前加载的包的问题:
x <- unlist(sapply(search()[-1],function(x)as.character(lsf.str(x))))查看评论就可以了。
I'd use
lsf.str()
as a start.eg :
x <- as.character(lsf.str("package:base"))
gives you a list of all functions in the base package. You could do add all packages you want to check against.stats
andutils
come to mind first.EDIT : Regarding your question about currently loaded packages :
x <- unlist(sapply(search()[-1],function(x)as.character(lsf.str(x))))see commentsdoes the trick.
我在很多个月前(2007 年)在 R-Help 上问了一个类似的问题,Brian Ripley 教授提供了这个解决方案:
给出的输出如下:
This was for find all the features in packages returned by object
pkgs
这样您就可以控制加载/检查哪些包。适用于当前加载的一组软件包的修改版本将是:
I asked a similar Q on R-Help many moons ago (2007) and Prof. Brian Ripley provided this as a solution:
Which gives output like:
This was for finding all the functions in packages specified by object
pkgs
so you can control which packages are loaded/checked against.A modified version that work on the currently loaded set of packages would be: