This guide was developed as way for me to take notes as I tried to convert a number of routines into a more easilly managed "package". It is geared to the Mac user who is clueless with respect to Windows and can stumble around (with some limited success) in the Unix part of the Mac.
An equally frustrated pair of users created this guide for Linux, Unix and Windows users. (Unfortunately, their page has a link for how to build packages for Macs and it refers back to this page. So, I can just recommend reading that page for other hints.)
Also, consult the R for Mac OS FAQ page which is the definitive resource for building packages, but does assume more ability than I seem to have to follow it completely.
Notes:
mylist = c (
"alpha.scale",
"correct.cor",
"cluster.cor",
"count.pairwise",
"describe",
"eigen.loadings",
"error.crosses",
"factor.congruence",
"factor.fit",
"factor.model",
"factor.residuals",
"factor.rotate",
"factor2cluster",
"fisherz",
"mat.regress",
"multi.hist",
"omega",
"paired.r",
"pairs.panels",
"panel.cor",
"panel.cor.scale",
"panel.hist",
"phi2poly",
"polychor.matrix",
"principal",
"psych", #name of package
"psycho.demo",
"read.clipboard",
"read.clipboard.csv",
"schmid",
"skew",
"summ.stats",
"VSS",
"VSS.parallel",
"VSS.plot",
"VSS.scree",
"VSS.simulate" )
psych <- function () {} # a dummy function to make it appear in the help menu
name = "psych"
package.skeleton(name, mylist, environment = .GlobalEnv,
path = ".", force = TRUE) #force = TRUE will overwrite prior file
1. Put any C/C++/Fortran code in 'src' 2. If you have compiled code, add a .First.lib() function in 'R' to load the shared library 3. Edit the help file skeletons in 'man' 4. Run R CMD build to create the index files 5. Run R CMD check to check the package 6. Run R CMD build to make the package file Read "Writing R Extensions" for more information.
Note, that the files in the man folder are written in a .Rd format. This format is used by R to create HTML and LaTex files. Although it is tempting to edit the html files that will eventually appear in the library, it turns out that one needs to learn to edit the Rd formated files. See section 2 of the package manual. The process of building the package takes these Rd files and converts them to HTML as well as Latex and (perhaps) pdf. As Rossi points out, it is vital to write clear documentation for your functions. This is, of course, the least fun part of program development, but essential.
The syntax of these .Rd files is moderately straight forward. The % mark signifies a comment. All commands start with a \ and are defined within { }.
An example description file
Package: psych Title: Procedures for Personality and Psychological Research Version: 1.0-7 Date: 2006-06-15 Author: William RevelleMaintainer: William Revelle Description: A number of routines for personality and experimental psychology, (This is my second draft of a package, so be patient,) Functions are primarily for scale construction and reliability analysis, although others are basic descriptive stats, For more information, see the personality-project.org/r License: GPL version 2 or newer URL: http://personality-project.org/r/
The package installer can load local packages (as source files) from your root directory. The installer then makes the html and pdf help files based upon your .Rd files.
If corrections to the package are to be made, they should be done in the original files in the original folder and then the build sequence can be followed again.
If you want to add any new functions to the package, you can save the function as foo.R in the R folder of the package and then add the appropriate help file (.Rd) in the man folder/directory. To create the new help file, use the prompt(foo) command. This will write foo.Rd into the man directory (folder).
After adding a new function (foo.R) to the foo.package directory and running prompt(foo.R) to create the foo.Rd file to be put in the foo.man directory, run R CMD check foo.package until no errors occur. (Be patient.)
Examine the Contents file and give keywords that match those found in the KEYWORDS.db file for R. Also add 1 line descriptions to each function. The index is created by the package manager, and uses the one line desciption files.
We now have a workable package. This can be tested and uploaded to Cran, or can be installed in a local "repository" for further testing and development. Note, that if the package is just written in R, it is loaded as source rather than as binary.
The use of repositories has been discussed in Rnews by Brian Ripley. Repositories are meant for development of department or university wide packages that might not be of general interest. I have been doing this for the psych package, trying to maintain both a PC and a Mac version. The Mac version has the requirement (according to the Mac FAQ): "A Mac OS X specific requirements, is that a prebuilt package is assumed to be named (and accordingly archived and compressed) as package_name.tgz. On Windows, for example, packages come in a zipped format."
To do this, we need to create a number of folders (directories if using a PC) and files. The basic default location for my repository is
Appropriate read permissions need to be added to these files and folders to allow others to access them.
Once this is done once, it is then possible to do direct edits to this set of files to make improvements to the package. However, this is a bad idea. It is much better to make the corrections to the package master folder (the pre built one, not the installed one), for that corrects the help files as well.
Once the habit of correcting .Rd files and function.R files becomes routinized, then the rebuilding operation is fairly straight forward.
R CMD check psych is a very useful (although tedious) operation. This will work through mistake after mistake in the .Rd files. Correct these!
Using X-11 to finish
Then, finally, we move the psych.1.0-7.tgz file to the PACKAGEs folder and it is ready to be installed by other Mac users.
I think it is preferred to actually do this to the result of the build operation rather than the folder inside the library.
To allow PC users to use the package, they need to be able to download the zip file. Thus, they need to be able to find it! I have created a folder r/bin/pcos that has the psych package. Presumably some introductory remarks similar to those found in the Mac package structure would be helpful.
How to set up a repository using BioConductor is discussed by the bioconductor folk, (but I am unable to make this work). http://www.maths.lth.se/help/R/.R/library/reposTools/doc/reposServer.pdf
With my package "psych" installed on the personality-project.org server, one can install it by using the R-package installer and specifying "other respository" with the address http://personality-project.org/r . It magically works its way down the file structure to get the appropriate package.