Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaning up function list in an R package with lots of functions

Tags:

r

[Revised based on suggestion of exporting names.] I have been working on an R package that is nearing about 100 functions, maybe more.

I want to have, say, 10 visible functions and each may have 10 "invisible" sub-functions.

Is there an easy way to select which functions are visible, and which are not?

Also, in the interest of avoiding 'diff', is there a command like "all.equal" that can be applied to two different packages to see where they differ?

like image 800
Iterator Avatar asked Jun 21 '11 15:06

Iterator


2 Answers

You can make a file called NAMESPACE in the base directory of your package. In this you can define which functions you want to export to the user, and you can also import functions from other packages. Exporting will make a function usable, and import will transfer a function from another package to you without making it available to the user (useful if you just need one function and don't want to require your users to load another package when they load yours).

A trunctuated part of my packages NAMESPACE :

useDynLib(qgraph)
export(qgraph)
(...)
importFrom(psych,"principal")
(...)
import(plyr)

which respectively loads the compiled functions, makes the function qgraph() available, imports from psych the principal function and imports from plyr all functions that are exported in plyr's NAMESPACE.

For more details read:

http://cran.r-project.org/doc/manuals/R-exts.pdf

like image 176
Sacha Epskamp Avatar answered Nov 14 '22 23:11

Sacha Epskamp


I think you should organise your package and code the way you feel most comfortable with; it is your package after all. NAMESPACE can be used to control what gets exposed or not to the user up-front, as other's have mentioned, and you don't need to document all the functions, just the main user-called functions, by adding \alias{} tags to the Rd files for all the support functions you don't want people to know too much about, or hide them on an package.internals.Rd man page.

That being said, if you want people to help develop your package, or run with it and do amazing things, the better organised it is the easier that job will be. So lay out your functions logically, perhaps one file per function, named after the function name, or group all the related functions into a single R file for example. But be consistent in which approach you do.

If you have generic functions that have more general use, consider splitting those functions out into a separate package that others can use, without having to depend on your mega package with the extra cruft that is more specific. Your package can then depend on this generic package, as can packages of other authors. But don't split packages up just for the sake of making them smaller.

like image 30
Gavin Simpson Avatar answered Nov 14 '22 23:11

Gavin Simpson