This tutorial will take you through the steps of creating a new package in R.

There are five primary tasks:

  1. Create a package skeleton (set of empty files and folders).
  2. Add your R code to the skeleton directory.
  3. Create documentation for your package (similar to knitting RMD files).
  4. Install your new package on your computer and test it out.
  5. Upload the package to GitHub.

The examples below will walk you through the steps. You will also need to download the Monty Hall Problem functions that we completed during Labs 01 and 02. This script contains some roxygen text to get you started with the process of documentation.

Download R Script

Install Development Packages

You can check that you have everything installed and working by running the following code:

## Your system is ready to build packages!

Document Your Functions

Add your roxygen comments to your R scripts:

Note that good documentation describes all of the arguments needed by the function, including the required data types of each object. And clearly describe what will be returned when the function runs (type of object, what it contains).

Place your documented R scripts into the “R” folder in your package directory, then try:

You should see:

You will see some errors as well if you have not yet finished documenting your functions. Ignore them for now.

You will now have a new folder in your montyhall directory called “man”, short for “manuals”. The documentation files have an .Rd (R documentation) extension.

Install Your Package

Go up one level in your directory so you are outside of the package folder, but in the same folder where the package folder lives. Then run the command to install your new package.

If successful you will see messages like this:

Try It Out!

In a new R session try:

## [1] "goat" "car"  "goat"

Check Help Files

You should be able to preview the help files that you created with your roxygen comments.

GitHub Hosting

You have a couple of options for sharing your new package with others. You could submit the package to the CRAN so that everyone in the world could install it in R using install.packages(“montyhall”).

We cannot use this option because (1) it’s a homework assignment so we don’t want to burden people with the task of reviewing a new package, (2) package names on the CRAN must be unique so everyone from the class would have to name it something different, and (3) the CRAN requires that the package passes some robustness checks to ensure everything is documented correctly and all of the code is running smoothly.

A simpler option is to host your package on GitHub. Complete the following steps to upload your code to a new repository on GitHub:

  1. Install the git client on your computer: LINK.
  2. Install GitHub Desktop application: LINK.
  3. Open GitHub in a browser and navigate to your profile.
  4. Create a new repository titled “montyhall”. Check the “Create with README” option.
  5. Copy the URL for your new repository.
  6. Open GitHub desktop and select File >> Clone Repository.
  7. Select the desired location of the folder and paste the URL into the appropriate dialogue.
  8. Clone your GitHub repo.
  9. Open the new “montyhall” folder that was just created by GitHub on your machine.
  10. Transfer all of the files inside of your old “montyhall” folder into the GitHub version.
  11. You should see the files appear in the GitHub Desktop app. In the “summary” field type something like “initial commit” and at the bottom left select “Commit to master”.
  12. Now up at the top select “Push origin”.

This last step sends all of the files to GitHub. They sould appear on your repo page shortly.

Now others should be able to install your package by typing: