R Tutorial: Introduction to package building

Amy Gott and Nick Crane from Mango Solutions walk through building R packages from scratch. You need four things: an R directory for code, a man directory for docs, a NAMESPACE file, and a DESCRIPTION file. The devtools and roxygen2 packages automate most of the work.
Chapters
Hi, I'm Amy Gott.
And I'm Nick Crane.
We're data Science consultants at Mango Solutions, and we'll be your instructors for this Introduction to Package Building course. Whether you want to share code across your business with the rest of the R community or just want a structured way to develop for yourself, R packages are a great way to do this. In this course, we will show you how you can easily create your own R packages, from the basics to writing documentation and even testing your code.
R packages can contain a variety of objects such as functions and data. In addition, they also contain help documentation, which typically includes examples of how to use individual functions. They can also include vignettes, which are longer descriptions or user guides of how to work with the package. And they can include tests for the functions in the package as well as much more. All of this is included in the package in a strictly defined structure, but.
You don't have to include all of this. In fact, there are only four things that must be in a package. Firstly, the R directory, where you put all of your R code. Then there's the man directory, where all of the documentation is stored. The namespace file contains information about functions that your package imports from other packages and functions that your package makes available to its users. Finally, the Description file contains metadata about your package, like who wrote it, what it does, its current version, and what its dependencies are.
Thankfully, we don't need to create all of this by hand. In this course, we're going to use some packages designed specifically to make package building more accessible, starting with the DevTools package. That will help us ensure we stick to the right package structure. We will also be using the roxygen2 package to make it simple to create the documentation for the man directory, as well as manage the namespace file. The only thing you will need to do is write your own functions and update the information in the template description file.
Throughout this course, you will use the DevTools package to not only manage the structure of the package, but also make development tasks like building and documenting simple. The aim of DevTools is to make package development as easy as possible for you, the package developer, so that you can spend more time on your code. All of the functions in DevTools are intuitively named, so they are very easy to remember. We're going to start by taking a look at the Create function, and we'll be visiting the rest as you progress through the course.
The Create function will create the basic package structure for us, generating a namespace file, description file, and the R directory it doesn't create the man directory yet, you'll see that that happens later. To use Create, all you need to do is tell it where to create the package and what name it should have. In this example, we will create a package called simutils in a new folder in the current working directory, the name of the folder you provide becomes the name of your package. Take care when naming your package. In particular, avoid names already taken on cran and that's it. Now you can start to write functions and store them in the R directory.
So now it's time for you to start writing an R package.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







