R Package

In this lab, we will learn how to build an R package.

Goal: by the end of this lab, you should be able to build an R package.

Syncing your fork

Open the ourpackage Project in RStudio that you cloned last week.

First, we have to pull all the changes that have been made to this repository since you cloned it! If you don’t do this, you won’t be able to make further contributions because your fork (origin) is not up to date with the shared repository (upstream).

  1. Sync your fork by executing at the Terminal:
git pull upstream main

If you see errors, read them and then ask me.

Building a package

An R package must have certain files in certain places in order to be valid. Additionally, RStudio has to know that the RStudio Project that you are working on is an R package.

Every R package needs a DESCRIPTION file. This file contains the basic information about the package that allows it to be compiled as a package. If you synced your fork correctly in the previous step, you should already have a DESCRIPTION file.

  1. Open the DESCRIPTION file and read it.

Next, we’re going to build and install the package. We do this by clicking on the “Build” tab in RStudio and then clicking on the “Install” button. [If you don’t see the Build tab, try closing the Project and then re-opening it. ] This is equivalent to running devtools::install().

First, go to “Tools -> Project Options… -> Build Tools” and make sure that:

  • Generate documentation with Roxygen is checked
  • When you click on “Configure…”, “Automatically roxygenize when running: Install and Restart” is checked!
  1. Use the “Install” button under the “Build” tab to build the package.
  2. Use the devtools::install() function to build the package.

You should now see a package named ourpackage in your Packages tab. If you don’t, something went wrong. Ask for help!

  1. Experiment by changing some of the information about the package in the DESCRIPTION file, and then rebuilding the package. Do you see the changes reflected in the Package documentation? Try changing the title, description, or version number. [Do NOT change the name of the package!]

Contributing to the package

  1. Edit the DESCRIPTION and add your name

  2. Add the print_emoji() method you wrote for the S3 lab to the file named print_emoji.R in the R/ directory. Rebuild the package.

Try to use your function by running (something like):

class(mtcars) <- c("beanumber", class(mtcars))
mtcars

Does it work?

  1. Add the @export directive to your method. Rebuild the package. Check the NAMESPACE file. Does it work now?

  2. If your method calls function from another package, add those dependencies to the Imports field in DESCRIPTION using use_package(). Rebuild the package.

  3. Use roxygen2 to document your method. Rebuild the package.

  4. Run an R CMD check by by clicking on the “Check” button under the “Build” tab. Continue until you have fixed all errors, warnings, and notes.

  5. Contribute your new print_emoji() method to the shared repository by opening a pull request!

Engagement

Prompt: Paste the errors, warnings, or messages that you get from R CMD check to the #questions channel. What questions do you have about what those mean?