Show names of everything in a package

后端 未结 4 1732
太阳男子
太阳男子 2020-12-03 03:31

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 tha

4条回答
  •  囚心锁ツ
    2020-12-03 04:07

    I know this is an older question, but I recently created a script that works well for getting this information. It will take a vector of package names get the following information for every function within each package. The package name, the function name, the str() for the function, and the description text from the help file for each function. Code is below in case if anyone is interested.

    available.packages <- installed.packages()
    suppressMessages(if(!"XML" %in% available.packages) install.packages("XML",repos = "https://cran.cnr.berkeley.edu/"))
    library(XML)
    
    # Function to get data from help console
    help_console <- function(topic, format=c("text", "html", "latex", "Rd"),
                             lines=NULL, before=NULL, after=NULL, pkg) {  
      format=match.arg(format)
      if (!is.character(topic)) topic <- deparse(substitute(topic))
    
      helpfile <- tryCatch({
        helpfile <- utils:::.getHelpFile(help(topic, package = as.character(pkg)))
      }, error = function(err) {
        helpfile <- NA
      }) 
    
      if(!is.na(helpfile[1]))
        {hs <- capture.output(switch(format, 
                                     text=tools:::Rd2txt(helpfile),
                                     html=tools:::Rd2HTML(helpfile),
                                     latex=tools:::Rd2latex(helpfile),
                                     Rd=tools:::prepare_Rd(helpfile)))
    
         if(!is.null(lines)) hs <- hs[lines]
         hs <- c(before, hs, after)
         html <- paste(hs, sep="\n", collapse = "")
      } else{
        html <- NA
      }
    
      return(html)
    }
    
    # Function to retrieve information on functions within a package
    get.funcs <- function(pkg){
      if(pkg %in% available.packages){
        print(pkg)
        require(pkg, character.only = TRUE)
    
        df <- as.data.frame(as.vector(lsf.str(paste0("package:", pkg))), stringsAsFactors = FALSE)
        colnames(df) <- "function.name"
    
        df$usage <- sapply(df$function.name, function(x) capture.output(str(get(x)))[1])
        df$package <- pkg
    
        df$description <- mapply(function(x,y) {
                                 html <- help_console(x, "html", lines = 1:25, before = "", after = "", pkg = y)
                                 if(!is.na(html)){
                                   doc <- htmlParse(html, asText = TRUE)
                                   plain.text <- xpathSApply(doc, "//p", xmlValue)[1]
                                 } else{
                                   plain.text <- NA
                                 }
                                 },
                                 df$function.name,
                                 df$package)
    
        return(df[,c("package","function.name","usage","description")])
      } else{
        print(paste(pkg, "- has not been installed and cannot be processed!"))
        return(NULL)
      }
    }
    
    
    # Create a vector of packages to process
    packages <- c("base","dplyr","lubridate","plyr","readr","readxl","reshape2","RODBC","stringr","tidyr","XLConnect","xlsx","XML","zoo")
    
    # Create a list of dataframes containing package information
    ldf <- lapply(packages, get.funcs)
    
    # Combine all dataframes in the list to a single dataframe
    df <- do.call(rbind, ldf)
    

提交回复
热议问题