How to find all functions in an R package?

≡放荡痞女 提交于 2019-12-17 06:09:43

问题


What is the best way to find all the functions associated in a package?? I am currently going through the caTools package. If I do ?caTools or ??caTools I am simply going to get search for functions called that but not the functions in the package. Is there an easy way to access all the functions in the R gui? Are there any good ways to search for functions?


回答1:


I am guessing that you are just looking for help(package = caTools), which will open your browser to the relevant help page that lists all the functions in the "caTools" package.

You can also try: library(help = caTools), but that doesn't seem to capture everything. The nice thing about this latter approach is that you can capture the output in case you needed to refer to it somewhere else:

x <- library(help = caTools)
x$info[[2]]
#  [1] "LogitBoost              LogitBoost Classification Algorithm"          
#  [2] "base64encode            Convert R vectors to/from the Base64 format"  
#  [3] "caTools-package         Tools: moving window statistics, GIF, Base64,"
#  [4] "                        ROC AUC, etc."                                
#  [5] "colAUC                  Column-wise Area Under ROC Curve (AUC)"       
#  [6] "combs                   All Combinations of k Elements from Vector v" 
#  [7] "predict.LogitBoost      Prediction Based on LogitBoost Classification"
#  [8] "                        Algorithm"                                    
#  [9] "read.ENVI               Read and Write Binary Data in ENVI Format"    
# [10] "read.gif                Read and Write Images in GIF format"          
# [11] "runmad                  Median Absolute Deviation of Moving Windows"  
# [12] "runmean                 Mean of a Moving Window"                      
# [13] "runmin                  Minimum and Maximum of Moving Windows"        
# [14] "runquantile             Quantile of Moving Window"                    
# [15] "runsd                   Standard Deviation of Moving Windows"         
# [16] "sample.split            Split Data into Test and Train Set"           
# [17] "sumexact                Basic Sum Operations without Round-off Errors"
# [18] "trapz                   Trapezoid Rule Numerical Integration"   



回答2:


You can get all the objects in your package with:

ls("package:caTools")

You can get all the function signatures in your package with:

lsf.str("package:caTools")



回答3:


If you want all exported functions (i.e. functions accessible via ::), then getNamespaceExports(pkgName) will do the trick.

If you want all functions in the package, including the ones accessible via :::, you can do ls(getNamespace(pkgName)).

As an example, with the stringr package:

getNamespaceExports("stringr")
[1] "fixed"           "ignore.case"     "invert_match"    "perl"            "str_c"               "str_count"       "str_detect"      "str_dup"         "str_extract"    
[10] "str_extract_all" "str_join"        "str_length"      "str_locate"      "str_locate_all"  "str_match"       "str_match_all"   "str_pad"         "str_replace"    
[19] "str_replace_all" "str_split"       "str_split_fixed" "str_sub"         "str_sub<-"       "str_trim"        "str_wrap"        "word" 

However, we know that stringr:::is.perl exists in the package, and as you can see:

setdiff(ls(getNamespace("stringr")), getNamespaceExports("stringr"))
[1] "case.ignored"    "check_pattern"   "check_string"    "compact"         "is.fixed"        "is.perl"         "match_to_matrix" "re_call"         "recyclable"     
[10] "re_mapply"   

So, we see that ls(getNamespace("stringr")) contains all of getNamespaceExports("stringr") in addition to the ::: functions.




回答4:


The pacman package (CRAN) (Dev Version: GitHub) works well for this. Specifically the p_funs function.

The syntax is:

p_funs(caTools)  # exported
p_funs(caTools, TRUE)  # includes non-exported



回答5:


Another way is to use collidr package

library(collidr)
library(dplyr)
collidr::packages_and_functions_dataframe %>% filter(package_names  == "caTools")

#    package_names     function_names
# 1        caTools    caTools-package
# 2        caTools       base64encode
# 3        caTools       base64decode
# 4        caTools             colAUC
# 5        caTools              combs
# 6        caTools         LogitBoost
# 7        caTools predict.LogitBoost
# 8        caTools          read.ENVI
# 9        caTools         write.ENVI
# 10       caTools           read.gif
# 11       caTools          write.gif
# 12       caTools             runmad
# 13       caTools            runmean
# 14       caTools             runmin
# 15       caTools             runmax
# 16       caTools        runquantile
# 17       caTools              runsd
# 18       caTools       sample.split
# 19       caTools          sumexact,
# 20       caTools        cumsumexact
# 21       caTools              trapz



来源:https://stackoverflow.com/questions/20535247/how-to-find-all-functions-in-an-r-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!