R scripting is very flexible. I can write a code and run it on the fly by the terminal. I can read a source file using source() function. Or I can load a package file using library() function.
A benefit of using a package is the easy management. A package can be managed as library at local disk or repository such as CRAN. So making a package from a matured R script is a good way, which will improve the ease of use.
Now, I am trying to make my first R package. Showing my scrabble here.
Let’s make a traditional Hello World package. The package name is HelloWorld, and that contains only a function hello.world().
From the R terminal.
1. write the code directly.
hello.world <- function(to.whom='world') cat('Hello,', to.whom, fill=T)
A HelloWorld folder is created at the R temporary folder, and the package files are put in it.
These files are almost done, but not completed. Editing some of them are required before going to the build step. Mainly, the help files in man folder are required to be updated.
The Rd format is described at the following page.
Most important section is the examples. Because the examples are live tests which are executed automatically by the check command, they must be real and compact.
After editing files, use a console.
R CMD build C:\bin\R\tmp\HelloWorld
Specify the path to the skeleton HelloWorld. This will create a HelloWorld_1.0.tar.gz file in current folder.
R CMD check HelloWorld_1.0.tar.gz
This will create a HelloWorld.Rcheck folder in current folder
where the new package is expanded and files are created to check. In Windows, usually, the utility pdflatex is not installed. So the check will fail on creating a help pdf file. But this error can be ignored.
After the check is done, the new package can be loaded from the test folder.
Now, the new hello.world() function in the package works, and ? hello.world shows the new help file for the function.