Building a package in RStudio is actually very easy

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.

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

7 thoughts on “Building a package in RStudio is actually very easy

  1. And if you really don’t like GUIs, its just require(devtools);create(“foo”); to make a boilerplate package structure. Edit stuff in the R folder, then load_all(“foo”) to load it. Repeat until done. Github integration is extra!

  2. Ah, but I love GUIs. What’s brilliant about RStudio is that you get the best of both worlds. A command line when you feel like it and menus and dialog boxes when you don’t. There are times when I just can’t be arsed to spell or type and that’s when I love a GUI.

  3. This is a great explanation. I always assumed there was a lot more to programming a package in R. I’m not sure I’ll ever need to do this but it’s nice to know that I could. Thanks!

    1. 2.15.2 is the version that I’m running. If I’m not mistaken, you only need rtools for a package-build on Windows. If you’re running Mac or Unix, you’re sorted. I installed rtools on Tuesday and am able to build and load the MRMR package. Have your tried installing rtools and performing a build? Do you get an error?

  4. Pingback: metvurst

Leave a Reply

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

You are commenting using your 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