显示包中所有内容的名称

时间:2012-08-24 17:52:47

标签: r r-package

是否有一种简单的方法可以在R中列出包中的所有内容? 例如,如果我输入foreach:::并点击 tab 两次,我可以看到那里的所有内容。
我怎么能得到这些物品的名字?

注意,ls("package:foreach", all.names=TRUE)不会显示.foreachGlobals

之类的内容

4 个答案:

答案 0 :(得分:24)

ls("package:foreach", all.names=TRUE)仅显示附加到搜索路径的内容,该路径仅包含从命名空间导出的对象。对ls的结果使用getNamespace代替:

ls(getNamespace("foreach"), all.names=TRUE)

答案 1 :(得分:4)

我发现utility that I've been using对此有用(并且还提供了一些其他有用的东西),例如:

ls(get('.__NAMESPACE__.', envir = asNamespace('ggplot2', base.OK = FALSE),
       inherits = FALSE))
[1] "dynlibs"   "exports"   "imports"   "lazydata"  "path"      "S3methods" "spec"     

访问NAMESPACE中的所有导出和内部变量,即

ggplot2:::

lsp(ggplot2)

# [1] ".__C__Scales"                     ".__global__"                     
# [3] ".__NAMESPACE__."                  ".__S3MethodsTable__."            
# [5] ".all_aesthetics"                  ".base_to_ggplot"                 
# [7] ".element_tree"                    ".onAttach"                       
# [9] ".packageName"                     ".plot_store" 
# ...

仅访问已导出的内容,即

ggplot2::

lsp(ggplot2, 'exports')

# $exports
# [1] "%+%"                       "%+replace%"               
# [3] "aes"                       "aes_all"                  
# [5] "aes_auto"                  "aes_q"                    
# [7] "aes_string"                "annotate"                 
# [9] "annotation_custom"         "annotation_logticks"    
# ...

用于库路径

lsp('ggplot2', 'path')

# $path
# [1] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/ggplot2"

包含在包

中的数据
lsp('ggplot2', 'lazydata')

# $lazydata
# [1] "diamonds"     "economics"    "midwest"      "movies"       "mpg"         
# [6] "msleep"       "presidential" "seals"  

用于S3方法

lsp('ggplot2', 'S3methods')

# $S3methods
# [,1]                         [,2]                       [,3]                              
# [1,] "+"                     "gg"                       "+.gg"                            
# [2,] "["                     "uneval"                   "[.uneval"                        
# [3,] "as.character"          "uneval"                   "as.character.uneval"             
# [4,] "autoplot"              "default"                  "autoplot.default"                
# [5,] "coord_aspect"          "default"                  "coord_aspect.default"            
# [6,] "coord_aspect"          "fixed"                    "coord_aspect.fixed"              
# [7,] "coord_aspect"          "map"                      "coord_aspect.map"                
# [8,] "coord_aspect"          "polar"                    "coord_aspect.polar"              
# [9,] "coord_aspect"          "quickmap"                 "coord_aspect.quickmap" 
# ...

看一切

lsp('ggplot2')

# pages and pages

代码:

lsp <- function(package, what, pattern) {
  if (!is.character(substitute(package)))
    package <- deparse(substitute(package))
  ns <- asNamespace(package)
  if (missing(pattern))
    pattern <- '.*'

  ## base package does not have NAMESPACE
  if (isBaseNamespace(ns)) {
    res <- ls(.BaseNamespaceEnv, all.names = TRUE)
    return(res[grep(pattern, res, perl = TRUE, ignore.case = TRUE)])
  } else {
    ## for non base packages
    if (exists('.__NAMESPACE__.', envir = ns, inherits = FALSE)) {
      wh <- get('.__NAMESPACE__.', inherits = FALSE,
                envir = asNamespace(package, base.OK = FALSE))
      what <- if (missing(what)) 'all'
      else if ('?' %in% what) return(ls(wh)) 
      else ls(wh)[pmatch(what[1], ls(wh))]
      if (!is.null(what) && !any(what %in% c('all', ls(wh))))
        stop('\'what\' should be one of ',
             paste0(shQuote(ls(wh)), collapse = ', '),
             ', or \'all\'', domain = NA)
      res <- sapply(ls(wh), function(x) getNamespaceInfo(ns, x))
      res <- rapply(res, ls, classes = 'environment',
                    how = 'replace', all.names = TRUE)
      if (is.null(what))
        return(res[grep(pattern, res, perl = TRUE, ignore.case = TRUE)])
      if (what %in% 'all') {
        res <- ls(getNamespace(package), all.names = TRUE)
        return(res[grep(pattern, res, perl = TRUE, ignore.case = TRUE)])
      }
      if (any(what %in% ls(wh))) {
        res <- res[[what]]
        return(res[grep(pattern, res, perl = TRUE, ignore.case = TRUE)])
      }
    } else stop(sprintf('no NAMESPACE file found for package %s', package))
  }
}

我也喜欢它,因为它显示了rapply的有用性:}

答案 2 :(得分:1)

Revolution R Enterprise(免费供学术使用)是R的编辑器/调试器/ GUI,默认情况下启动时会打开“对象浏览器”窗口。单击包可以查看其所有内容,包括类和环境。我觉得这很有帮助。

答案 3 :(得分:0)

我知道这是一个较旧的问题,但我最近创建了一个适合获取此信息的脚本。它将采用包名称向量获取每个包中每个函数的以下信息。包名称,函数名称,函数的str()以及每个函数的帮助文件中的描述文本。如果有人有兴趣,代码如下。

{ 
    "_id" : 1.0, 
    "tags" : [
        "technology"
    ], 
    "musings" : {
        "0" : {
            "rating" : 2.0
        }
    }
}