knitr::opts_chunk$set(comment = "")

Session details


  1. To see that making packages is not as difficult as it seems.
  2. To learn about and use the tools to simplify and automate package development.
  3. To provide the resources to expand your knowledge on package development.

At the end of this session you will be able:

  1. Setup up the necessary folders and files for making a package.
  2. Fill out some package description information (in the DESCRIPTION and files).
  3. Include some functions in your package (in the R/ folder).
  4. Install your package.

Resources for learning and help

List of functions and files covered



Creating and naming a package

First thing is first: An R package is simply a collection of R functions saved on your computer that make it easy to access by using the library() command. When you make an R package, it does not get uploaded online, it does not mean other people can use it right away. At the beginning, the R package is only available to you and to your computer. That's it. So why would you want to create a package? Well, it can save time later with other data analyses since you can access your functions across files by using library(<yourpackagename>). A package bundles related code together, simplifying your life! Alright, that's done with, next step!

Creating the initial setup of files and folders necessary for an R package is incredibly easy with the usethis package! But! Before we begin, it's a good idea to think about what the name of the package should be. You can name it whatever you want, as long as it is not "base", "stats", "graphics", "utils", "tools", "methods", or any other default base R package. Generally, name it something that is pretty unique or specific to you. For this session, let's call it by your initials (or mine "lwj") and add "helpers"... so:

pkg_name <- file.path(tempdir(), "lwj.helpers")

A bunch of things are shown on your console. These are just some information that usethis is telling you that it has done. Otherwise, you now have the beginning of an R package! Pretty easy eh??

RStudio project options

Before we get into the package details more, let's make some edits to the project options. We'll change the "General" options to all "No" and check that the "Build Tools" has correctly set the project as a package.

Files and folders

There are many things that make an R package, well, an R package. One of them is the folder and file structure. All R packages must have certain files and folders in order to properly install.


Optional, but very helpful:

Package metadata

As mentioned above, there are two main files for providing package "metadata". Firstly, there is the DESCRIPTION, which is required for making a package. There are several data-fields in the file:

withr::with_dir(pkg_name, desc::desc())

The important data-fields are the Title, Authors, Description, and License (get to this more later). Ok, let's open up the file and make the edits.

Exercise (3-5 min): Open the DESCRIPTION file and change the title, author, and description fields.

The next optional, but important file to have as "human-readable metadata" is the file. Right now it doesn't exist. But we can create it using:

withr::with_dir(pkg_name, {

The file looks like this:

cat(readr::read_lines(file.path(pkg_name, "")), sep = "\n")
# cat(file = file.path(pkg_name, ""))

The file is a markdown file. We covered markdown in a previous session. Generally, this file is a brief documentation containing a high level summary of what the package does, how to install it, and sometimes a simple example of how to use it. It should be written and targetted to other (potential) users, or yourself 9 months in the future (you'll thank yourself). Ok, this file right now has some things that need to be changed. Let's edit it.

Exercise (5 min): Write a (fake) goal and description of the package in the file. Remove the text in the "Installation" section for now. Delete the "Example" section.

Next we want to add the license. The license is really important, even if you never put your package online. The license gives information about how other people can use and modify your code. We won't cover the legal details about licensing, but for now, I would recommend using an MIT License:

# usethis::use_mit_license("<your name>")
usethis::use_mit_license("Luke Johnston")
withr::with_dir(pkg_name, {
    usethis::use_mit_license("Luke Johnston")

The contents of the LICENSE file are:

cat(readr::read_lines(file.path(pkg_name, "LICENSE")), sep = "\n")

Adding R code

Now for the main reason you have created a package! To put your R code in it so you can use the code easily in other scripts! To create an R script in the package, use:

# usethis::use_r("<filename>")
# usethis::use_r("<filename>")
withr::with_dir(pkg_name, {

An R script will open in RStudio. Now, let's add a function to it. We'll use a very simple function in file:

addition <- function(x, y) {
    x + y

Now save. To test that it works, now we run the next command:


Or type "Ctrl-Shift-L". In the console, test if the function works:

addition(2, 2)

It should work! We'll add some more functions and play around with the R scripts.

Building and installing

Now, the final step! Let's build and install the package! When you feel your package is ready, time to install it for you to use with library()! And you can install it by using...:


Or by typing "Ctrl-Shift-B". It does some quick checks, and if all goes well, you now have installed your package!

Other useful functions

If you end up developing R packages often, I would recommend running this command:


This let's you add some code to automatically load when you open up R, to make it easier to continue your package development!

au-oc/content documentation built on May 21, 2019, 4:05 a.m.