ЯтомизоnoR

R, Statistics

Make an R package – Hello, world

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.

References

Process flow

  1. source or write the R script function.
  2. create a new package using the package.skeleton() function.
  3. edit the package files.
  4. build a package using the R CMD build command.
  5. check the package using the R CMD check command.

Hello World

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)

2. package.skeleton()

package.skeleton(name='HelloWorld', list=c('hello.world'))

A HelloWorld folder is created at the R temporary folder, and the package files are put in it.

3. editing

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.
http://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation-files

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.

package files of HelloWorld

Fig. 1 package files of HelloWorld

4. build

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.

5. check

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.

6. test

After the check is done, the new package can be loaded from the test folder.

library(package=HelloWorld, lib.loc='C:\\tmp\\HelloWorld.Rcheck')

Now, the new hello.world() function in the package works, and ? hello.world shows the new help file for the function.

Advertisements

2 comments on “Make an R package – Hello, world

  1. Pingback: Making of elliplot package | ЯтомизоnoR

  2. Pingback: Where to begin MacTeX | ЯтомизоnoR

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on August 28, 2013 by and tagged , , , .
The stupidest thing...

Statistics, genetics, programming, academics

ЯтомизоnoR

R, Statistics

%d bloggers like this: