Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show names of everything in a package

Tags:

Is there an easy way to list everything in a package from within R?
For example, if I type foreach::: and hit tab twice, I can see everything that's there.
How else can I get those names of objects?

Note, ls("package:foreach", all.names=TRUE) does not show things like .foreachGlobals

like image 478
GSee Avatar asked Aug 24 '12 17:08

GSee


People also ask

How do I see all functions in a package in R?

Data Visualization using R Programming We can find the functions inside a package by using lsf. str function but we need to load the package prior to knowing the functions inside.

Which command is used to see all particular library?

You can use lsf.

How do I see what packages are in R?

To see what packages are installed, use the installed. packages() command.

What does the library function do in R?

In R, a package is a collection of R functions, data and compiled code. The location where the packages are stored is called the library. If there is a particular functionality that you require, you can download the package from the appropriate site and it will be stored in your library.


2 Answers

ls("package:foreach", all.names=TRUE) only shows what's attached to the search path, which only includes the objects exported from the namespace. Use ls on the results of getNamespace instead:

ls(getNamespace("foreach"), all.names=TRUE)
like image 182
Joshua Ulrich Avatar answered Sep 20 '22 20:09

Joshua Ulrich


A utility that I've been using which I find useful for this (and also gives some other helpful things) such as:

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

to access all exported and internal variables in the NAMESPACE, ie,

ggplot2:::

lsp(ggplot2)

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

to access only exported, ie,

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"    
# ...

for the library path

lsp('ggplot2', 'path')

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

for data included in packages

lsp('ggplot2', 'lazydata')

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

for S3 methods

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" 
# ...

to see everything

lsp('ggplot2')

# pages and pages

code:

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))
  }
}

I also like it because it shows how useful rapply can be :}

like image 41
rawr Avatar answered Sep 18 '22 20:09

rawr