Making an R Package: Not as hard as you think

[This article was first published on Drunks&Lampposts » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve been writing functions in R for a while to do various things like talking to APIs, web scraping, model testing and data visualisation (basically thing which can get a bit repetitive!), but have always been slightly intimidated about turning those functions into a package, which I could then call using library (package-name). Note that I’m writing this from a Windows perspective, but should work just as well on other platforms I think (you just won’t need to install Rtools.

I finally took the leap today and created my first package… and it turned out to not be too hard to do. This guide by Rob Hyndman is a good place to start, as it covers the key steps that you need to go through.

There are three key steps to building a package (assuming that you have already written the functions that are to be included in the package!).

  1. Create a skeleton package.
  2. Fill in the Documentation and Help Files.
  3. Use Rcmd to build and install the package.

1. Create a skeleton package

 package.skeleton("package-name") 

will get you started, by building a base package which contains the objects that are currently loaded into your R workspace (note that this will build the package into the current work directory, so set this prior to creating the skeleton).

2. Fill in the Documentation and Help Files.

After this has run successfully, you then need to provide information for DOCUMENTATION (things like author name, and also add on DEPENDS for any other packages that the package you’re creating depends on). You also need to write the help files for the functions, which are a set of .Rd files that are within the package/MAN folder.

3. Use Rcmd to build and install the package.

The final step is to actually build your package, and if you’re doing this on Windows then you need to have installed Rtools which is available from here.

Once this is installed, you need to use the command line to run the following code :

CD /to/file/path/package
Rcmd INSTALL --build packagename

This will install the package and also create a ZIP file in the same folder as where the skeleton package has been created so that you can then distribute this to others (assuming that their on the same OS as you).

It’s then relatively easy to update the package with new functions or data:
1. Add the function to the r-package/R directory (where the other functions already sit)
2. The file should be named with .R as the extension
3. Create a new manual page (ending with the extension .Rd) in r-package/man directory (use one of the existing help files as a template).
4. If there are any new dependencies, add them to the “Depends:” line in DESCRIPTION.

There’s a lot more detail that can be gone into (e.g. S3 and S4 classes), but to get through the basics, the above and the guide from Rob Hyndman should sort you out.


To leave a comment for the author, please follow the link and comment on their blog: Drunks&Lampposts » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)