Question

How to distinguish helper functions from main functions in a package containing lot of functions? Is there a way to separate them by means of some standard identifier may be implemented in R?

For example, I am using sets package in my R session as follows. How would I separate helper functions from main functions from the list given below. I could do it by reading the documentation (sometimes it is explicitly documented a function as helper function), but is there is standard identifier implemented in R, to say that function is a helper or main function?

library(sets)

ls("package:sets")
[1] "%..%"                              "%<%"                               "%<=%"                             
[4] "%>%"                               "%D%"                               "%e%"                              
[7] "%is%"                              "as.cset"                           "as.gset"                          
[10] "as.interval"                       "as.set"                            "as.tuple"                         
[13] "binary_closure"                    "binary_reduction"                    "canonicalize_set_and_mapping"     
[16] "charfun_generator"                 "closure"                           "cset"                             
[19] "cset_bound"                        "cset_cardinality"                  "cset_cartesian"                   
[22] "cset_charfun"                      "cset_combn"                        "cset_complement"                  
[25] "cset_concentrate"                  "cset_contains_element"             "cset_core"                        
[28] "cset_defuzzify"                    "cset_difference"                   "cset_dilate"                      
[31] "cset_dissimilarity"                "cset_has_missings"                 "cset_height"                      
[34] "cset_intersection"                 "cset_is_crisp"                     "cset_is_empty"                    
[37] "cset_is_equal"                     "cset_is_fuzzy_multiset"            "cset_is_fuzzy_set"                
[40] "cset_is_multiset"                  "cset_is_proper_subset"             "cset_is_set"                      
[43] "cset_is_set_or_fuzzy_set"          "cset_is_set_or_multiset"           "cset_is_subset"                   
[46] "cset_matchfun"                     "cset_matchfun<-"                   "cset_mean"                        
[49] "cset_memberships"                  "cset_normalize"                    "cset_orderfun"                    
[52] "cset_orderfun<-"                   "cset_outer"                        "cset_peak"                        
[55] "cset_power"                        "cset_product"                      "cset_similarity"                  
[58] "cset_sum"                          "cset_support"                      "cset_symdiff"                     
[61] "cset_transform_memberships"        "cset_union"                        "cset_universe"                    
[64] "e"                                 "fuzzy_bell"                        "fuzzy_bell_gset"                  
[67] "fuzzy_cone"                        "fuzzy_cone_gset"                   "fuzzy_inference"                  
[70] "fuzzy_logic"                       "fuzzy_normal"                      "fuzzy_normal_gset"                
[73] "fuzzy_partition"                   "fuzzy_pi3"                         "fuzzy_pi3_gset"                   
[76] "fuzzy_pi4"                         "fuzzy_pi4_gset"                    "fuzzy_rule"                       
[79] "fuzzy_sigmoid"                     "fuzzy_sigmoid_gset"                "fuzzy_system"                     
[82] "fuzzy_trapezoid"                   "fuzzy_trapezoid_gset"              "fuzzy_triangular"                 
[85] "fuzzy_triangular_gset"             "fuzzy_tuple"                       "fuzzy_two_normals"                
[88] "fuzzy_two_normals_gset"            "fuzzy_variable"                    "gset"                             
[91] "gset_bound"                        "gset_cardinality"                  "gset_cartesian"                   
[94] "gset_charfun"                      "gset_combn"                        "gset_complement"                  
[97] "gset_concentrate"                  "gset_contains_element"             "gset_core"                        
[100] "gset_defuzzify"                    "gset_difference"                   "gset_dilate"                      
[103] "gset_dissimilarity"                "gset_has_missings"                 "gset_height"                      
[106] "gset_intersection"                 "gset_is_crisp"                     "gset_is_empty"                    
[109] "gset_is_equal"                     "gset_is_fuzzy_multiset"            "gset_is_fuzzy_set"                
[112] "gset_is_multiset"                  "gset_is_proper_subset"             "gset_is_set"                      
[115] "gset_is_set_or_fuzzy_set"          "gset_is_set_or_multiset"           "gset_is_subset"                   
[118] "gset_mean"                         "gset_memberships"                  "gset_normalize"                   
[121] "gset_outer"                        "gset_peak"                         "gset_power"                       
[124] "gset_product"                      "gset_similarity"                   "gset_sum"                         
[127] "gset_support"                      "gset_symdiff"                      "gset_transform_memberships"       
[130] "gset_union"                        "gset_universe"                     "integers"                         
[133] "integers2reals"                    "interval"                          "interval_complement"              
[136] "interval_contains_element"         "interval_difference"               "interval_division"                
[139] "interval_domain"                   "interval_intersection"             "interval_is_bounded"              
[142] "interval_is_closed"                "interval_is_countable"             "interval_is_degenerate"           
[145] "interval_is_empty"                 "interval_is_equal"                 "interval_is_finite"               
[148] "interval_is_greater_than"          "interval_is_greater_than_or_equal" "interval_is_half_bounded"         
[151] "interval_is_left_bounded"          "interval_is_left_closed"           "interval_is_left_open"            
[154] "interval_is_left_unbounded"        "interval_is_less_than"             "interval_is_less_than_or_equal"   
[157] "interval_is_proper"                "interval_is_proper_subinterval"    "interval_is_right_bounded"        
[160] "interval_is_right_closed"          "interval_is_right_open"            "interval_is_right_unbounded"      
[163] "interval_is_subinterval"           "interval_is_unbounded"             "interval_is_uncountable"          
[166] "interval_measure"                  "interval_power"                    "interval_product"                 
[169] "interval_sum"                      "interval_symdiff"                  "interval_union"                   
[172] "is.charfun_generator"              "is.cset"                           "is.gset"                          
[175] "is.interval"                       "is.set"                            "is.tuple"                         
[178] "is_element"                        "LABEL"                             "LABELS"                           
[181] "make_set_with_order"               "matchfun"                          "naturals"                         
[184] "naturals0"                         "pair"                              "reals"                            
[187] "reals2integers"                    "reduction"                         "set"                              
[190] "set_cardinality"                   "set_cartesian"                     "set_combn"                        
[193] "set_complement"                    "set_contains_element"              "set_dissimilarity"                
[196] "set_intersection"                  "set_is_empty"                      "set_is_equal"                     
[199] "set_is_proper_subset"              "set_is_subset"                     "set_outer"                        
[202] "set_power"                         "set_similarity"                    "set_symdiff"                      
[205] "set_union"                         "sets_options"                      "singleton"                        
[208] "triple"                            "tuple"                             "tuple_is_ntuple"                  
[211] "tuple_is_pair"                     "tuple_is_singleton"                "tuple_is_triple"                  
[214] "tuple_outer"  

Was it helpful?

Solution

Typically "helper" functions (defined as: functions used by main functions and typically not used by end users directly) are not exported and are not visible unless you use the package_name:::helper_function_name syntax. If a function is visible and documented it typically is a main function. So, if you want to see main functions, just use ls:

ls("package:sets")

If you want all functions (main + helper):

ls(getNamespace("sets"))

And finally, to get just helper functions, use setdiff:

setdiff(ls(getNamespace("sets")), ls("package:sets"))

See this SO Q/A for some discussion.

One potential ambiguity is that sometimes S3 methods are not explicitly exported, even if they are intended to be used as "main" functions.

OTHER TIPS

R library's NAMESPACE file should include a list of export - functions which are exposed. So, there are good definitions of exposed (thus public) and non-exposed (thus internal/private) functions. There is no formal definition of 'main' and 'helper' functions. You may argue only 'main' functions should be exposed, but I don't think it is clear.

A lazy developer sometimes includes exportPattern("^[[:alpha:]]+") in NAMESPACE file, thus exposing all functions. In that case, I might suspect that that library's exposed functions are all mixed with main/helper functions. However, when I checked sets's NAMESPACE file, it is very well organized. Thus, I believe sets's exposed functions are well maintained and necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top