Building a package in RStudio is actually very easy

January 30, 2013
By

(This article was first published on PirateGrunt » R, and kindly contributed to R-bloggers)

So, you’ve written some code and you use it routinely. Now you’ve written some code and you’d like to use version control to ensure that development continues in a robust fashion. You do that and you use Github or something so that not only are changes tracked, but the general public receives the benefit of your knowledge. At the same time, you receive the benefit of their editing and ideas, but in a secure way. Now what? All that useful, well-functioning code needs a home, some documentation and a little dignity. It needs to become a package.

This is actually very easy to do. I had a few false starts, but now that I know what’s going on, I can create an R package faster than you can say Hadley Wickham.

  1. Start with a Github repository. This will have a set of R sourcecode files, which collectively do something interesting.
  2. Within RStudio, create a new project, using your local Github repository as the project’s source directory.
  3. Within the “Build Tools” section of the “Project Options” make sure that you’ve selected “Package” from the “Project Build Tools” dropdown box.
  4. In that same dialog box, enter the “–no-examples” flag of the “Check Package” build options. (At this stage, you probably don’t have any examples composed. The default assumes that you do. I don’t know why this is.)
  5. If RStudio, didn’t create one, create a subdirectory in your project folder called “R”. Move all your code here. Github will reflect the deletion and new file creation.
  6. Create a DESCRIPTION file. There are numerous sources which explain how to do this.
  7. Make sure your code doesn’t have any errors in it. Easiest way to do that is to source all of them.
  8. Build the package.

And that’s it. If you press CTRL+SHIFT+B (in Windows, at any rate) RStudio will build the package and load it for you. All of your functions are now in memory, but won’t appear in the Workspace pane.

RStudio has fine- if brief- documentation on their site, with links to more detailed guidance. The most useful of these to me, so far, was the wiki for Wickham’s devtools package.

Of course, that’s only about half the story. You still have to write documentation for all your functions, provide a demo file, compose a vignette, etc. Then, you may want to submit to CRAN. For now, I’m just using Github, which will be fine for the forseeable future.

The MRMR project may now be loaded directly into R via Github. The documentation is non-existent for now. It’s a very bare bones application, so could be useful as a template for others. Meanwhile, Github has loads of good examples of R packages.

Want to load MRMR? Instructions below will make that happen.

library(devtools)
install_github(repo = "MRMR", username = "PirateGrunt")
library(MRMR)
df = GetNAICData()
head(df)

To leave a comment for the author, please follow the link and comment on his blog: PirateGrunt » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.