Let your heart guide you......It whispers so listen closely

Monday, February 18, 2013

Package making in R

Recently I was trying to make a package in R. As always I searched on internet. I realised that many people have discussed the technique of package making. But there are lot of complexities involved in the procedure, probably that is why I was stuck for too long. I went through many different manuals, but somehow, they were too much for my level. I wanted some very simple steps, very basic ones. Since I was a starter, so I wanted to get one in working condition and then refine it further. I found Rob J Hyn­d­man's discussion quite useful. I am presenting here a further simplified version (which worked for me) of his steps:

Step.1- Install R tools (available on the web).

Step.2 - Goto Control Panel -> System -> Advanced System Settings-> Environment Variables -> System Variables 
Edit the variable with name Path as follows:
Add C:\Program Files\R\R-2.15.1\bin\i386;c:\Rtools\bin; c:\Rtools\MinGW; c:\Perl\bin; (where first directory should be the one which contains R-executable) to already existing C:\Program Files\MiKTeX 2.7\miktex\bin; %SystemRoot%\system32; %SystemRoot%; %SystemRoot%\System32\Wbem; %SYSTEMROOT%\System32\WindowsPowerShell\v1.0\; C:\Program Files\MATLAB\R2008a\bin; C:\Program Files\MATLAB\R2008a\bin\win32

Step.3 - Goto DOS prompt and type the following
R.exe (R should boot up from any directory if above step is correct)

Step.4 - Create an R work­space con­tain­ing all the relevant functions and data sets that you want to include in the package. Delete anything from the work­space that you do not want to include in the package.
Set the current directory to where ever you want create the package.

Step.5 - To cre­ate a pack­age called basicpack, use the R command
This will gen­er­ate a direc­tory fred and sev­eral sub-​​directories in the required structure. A pack­age con­sists of a direc­tory con­tain­ing a file ‘DESCRIPTION’ and usu­ally has the sub­di­rec­to­ries R, data and man.The package.skeleton com­mand above will have cre­ated the required files and directories. You now need to edit them so they con­tain the right information. To start with one may not edit all of them. There is only one field which is compulsory to edit, without which you cannot proceed. Goto the directory where you have created the skeleton of the package, you will see a folder with package name i.e. basicpack -> man
You will see some R files. Each of which will have a \title{ } field, which will be empty. To install your package, you need make this field non-empty in each R-file.

Step.6 - For com­pil­ing goto DOS prompt in the direc­tory con­tain­ing your pack­age and type
Rcmd INSTALL --build basicpack

Step.7 - To check that the pack­age sat­is­fies the require­ments for a CRAN pack­age, use
Rcmd check basicpack
The checks are quite strict. A pack­age will often work ok even if it doesn’t pass these tests. But it is good prac­tice to build pack­ages that do sat­isfy these tests as it may save prob­lems later.

With this you have a package in working condition, which you can install from local zip file and it will be available in the list of Packages in your R window. The steps I have discussed, will only help you make a very basic and raw package. Offcourse there is lot more to package making. Like one needs to add descriptions, examples, add namespaces, datasets, bring dependency on other packages, help file, and much more. I think lot of information about these steps is available on internet. I have not explored any of these yet. Its like I was unable to stand, and people were giving me a mixed guide on how to stand, walk and run :(


Akanksha said...

Liked the post and loved the last line :)

It's like you are teaching someone to stand, by actually holding hand...

Post a Comment