有没有一种方法可以获取包含 R 中可以使用的所有函数名称的向量?

发布于 2024-10-04 08:52:16 字数 532 浏览 7 评论 0原文

我想要一个调用返回一个向量,其中包含我可以在当前 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

讽刺将军 2024-10-11 08:52:16

我会使用 lsf.str() 作为开始。

例如: x <- as.character(lsf.str("package:base")) 为您提供基础包中所有函数的列表。您可以添加所有要检查的包。首先想到的是 statsutils

编辑:关于您有关当前加载的包的问题:

x <- unlist(sapply(search()[-1],function(x)as.character(lsf.str(x)))) 查看评论

pkgs <- search()
pkgs <- pkgs[grep("package:",pkgs)]
y <- unlist(sapply(pkgs,lsf.str))

就可以了。

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 and utils 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 comments

pkgs <- search()
pkgs <- pkgs[grep("package:",pkgs)]
y <- unlist(sapply(pkgs,lsf.str))

does the trick.

心的憧憬 2024-10-11 08:52:16

我在很多个月前(2007 年)在 R-Help 上问了一个类似的问题,Brian Ripley 教授提供了这个解决方案:

findfuns <- function(x) {
     if(require(x, character.only=TRUE)) {
        env <- paste("package", x, sep=":")
        nm <- ls(env, all=TRUE)
        nm[unlist(lapply(nm, function(n) exists(n, where=env,
                                               mode="function",
                                               inherits=FALSE)))]
     } else character(0)
}
pkgs <- dir(.Library)
z <-  lapply(pkgs, findfuns)
names(z) <- pkgs
Z <- sort(unique(unlist(z)))

给出的输出如下:

> head(Z)
[1] "^"        "-"        "-.Date"   "-.POSIXt" ":"        "::"

This was for find all the features in packages returned by object pkgs 这样您就可以控制加载/检查哪些包。

适用于当前加载的一组软件包的修改版本将是:

findfuns2 <- function(pkgs) {
    nm <- ls(pkgs, all = TRUE)
    nm[unlist(lapply(nm, function(n) exists(n, where = pkgs,
                                            mode = "function",
                                            inherits = FALSE)))]
    if(isTRUE(all.equal(length(nm), 0)))
        character(0)
    else
        nm
}

pkgs <- search()
pkgs <- pkgs[grep("package:", pkgs)]
z <- lapply(pkgs, findfuns2)
z <- sort(unique(unlist(z)))
head(z)

I asked a similar Q on R-Help many moons ago (2007) and Prof. Brian Ripley provided this as a solution:

findfuns <- function(x) {
     if(require(x, character.only=TRUE)) {
        env <- paste("package", x, sep=":")
        nm <- ls(env, all=TRUE)
        nm[unlist(lapply(nm, function(n) exists(n, where=env,
                                               mode="function",
                                               inherits=FALSE)))]
     } else character(0)
}
pkgs <- dir(.Library)
z <-  lapply(pkgs, findfuns)
names(z) <- pkgs
Z <- sort(unique(unlist(z)))

Which gives output like:

> head(Z)
[1] "^"        "-"        "-.Date"   "-.POSIXt" ":"        "::"

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:

findfuns2 <- function(pkgs) {
    nm <- ls(pkgs, all = TRUE)
    nm[unlist(lapply(nm, function(n) exists(n, where = pkgs,
                                            mode = "function",
                                            inherits = FALSE)))]
    if(isTRUE(all.equal(length(nm), 0)))
        character(0)
    else
        nm
}

pkgs <- search()
pkgs <- pkgs[grep("package:", pkgs)]
z <- lapply(pkgs, findfuns2)
z <- sort(unique(unlist(z)))
head(z)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文