git pull upstream mainR 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).
- Sync your fork by executing at the Terminal:
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.
- Open the
DESCRIPTIONfile 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!
- Use the “Install” button under the “Build” tab to build the package.
- 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!
- Experiment by changing some of the information about the package in the
DESCRIPTIONfile, 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
Edit the
DESCRIPTIONand add your nameAdd the
print_emoji()method you wrote for the S3 lab to the file namedprint_emoji.Rin theR/directory. Rebuild the package.
Try to use your function by running (something like):
class(mtcars) <- c("beanumber", class(mtcars))
mtcarsDoes it work?
Add the
@exportdirective to your method. Rebuild the package. Check the NAMESPACE file. Does it work now?If your method calls function from another package, add those dependencies to the Imports field in
DESCRIPTIONusinguse_package(). Rebuild the package.Use
roxygen2to document your method. Rebuild the package.Run an
R CMD checkby by clicking on the “Check” button under the “Build” tab. Continue until you have fixed all errors, warnings, and notes.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 checkto the#questionschannel. What questions do you have about what those mean?