Making Your First R Package 📦

ResBazAZ 2023

Eric R. Scott

Communications & Cyber Technologies, University of Arizona

Why make an R package?

  • You find yourself copying and pasting code from one project to another

  • You have data set you want to document and share

  • Your R code will be helpful for others doing similar analyses and you want to share

What makes an R package?

  • DESCRIPTION

    • Metadata

    • Dependencies & requirements

  • R code (functions)

  • Data

  • Documentation

Overview

In the workshop today we will:

  1. Make an R package with a function, a data set, documentation, and more!
  2. Put that package on GitHub so it can be installed by others
  3. Add unit tests and set up automated testing with GitHub actions
  4. Give you the resources to learn more

Cover of R Packages book by Hadley Wickham.  An eagle perches on top of the title block

R Packages (2e) goes into more detail on just about everything we’ll cover today and more!

Cover image for rOpenSci Packages book

rOpenSci is a great organization that supports development of R packages. Their book is another great resource to learn more.

Naming your package

  • Short, no spaces hyphens or underscores

  • Should reflect what the package does to some extent

  • Shouldn’t be the name of an already existing package

Tip

Check for availability of package names with the available package

Note

Today we are making a toy package, so your package name doesn’t need to follow these suggestions!

Making a New Package

There are several ways to go about making a new R package that can be shared. We’re going to go with the “GitHub first” method today:

  1. Create an empty repository on GitHub with our package’s name
  2. Clone that repository as a new R project
  3. Convert that R project into an R package

Git and GitHub

  • We will be using git and GitHub in this workshop in it’s simplest ways

  • Git is a “version control” software that will let you track changes to files and “push” those changes to GitHub

  • GitHub is a website for hosting code “repositories” and it will be how people can find and install our R package

Tip

Check out Andrew Antaya’s session Collaborating on code with GitHub on Wednesday (track B)!

Needed packages

library(devtools)
library(here)
#if these lines give errors, you may need to install these packages:
# install.packages("devtools")
# install.packages("here")
  • devtools is a “meta package” kind of like tidyverse

  • here will only be used during the setup process for this workshop

Check for git

Check that RStudio knows where git is on your computer. Go to Tools > Global Options… and select the Git/SVN option.

Make sure the checkbox is checked and there is something in the “Git executable” box

Installing git

If there is nothing in the git executable box, it is possible you need to install git on your system:

macOS

In terminal:

xcode-select --install

Windows

Download the installer here: https://git-scm.com/download/win

Let’s Get Started!