8 Toy Package
8.1 Introduction
In this chapter you will learn how to quickly build an off-the-shelf R package (in less than a minute) following the default options provided by RStudio. I prefer to show you this option first, before describing how to create a package with the functions of our "coin"
and "toss"
objects.
8.2 An off-the-shelf package
Nowadays you can create an R package in an almost instant way. Here’s the list of steps to follow in RStudio that allows you to create the structure of a package from scratch.
- On the menu bar, click File and select New Project

Figure 8.1: Starting point for a new project
- Then choose New Directory

Figure 8.2: Different types of RStudio projects
- Choose R package
- Give it a name: e.g. “cointoss”

Figure 8.3: Choosing a name for a package
- The filestructure of your package will be created with some default content. Here’s a screenshot of how the panes in RStudio look like in my computer. Notice the default R script
hello.R
and the file structure in the Files tab:
8.3 Minimal Filestructure
If you look at pane with the Files tab, you should be able to see the following files and directories:

Figure 8.4: Minimal filestructure created by devtools
We’ve ended up with six components inside the package folder. Here’s the description of each file:
DESCRIPTION
is a text file (with no extension) that has metadata for your package. Simply put, this file is like the business card of your package.NAMESPACE
is a text file (also with no extension) that is used to list the functions that will be available to be called by the user.The
R/
directory which is where you store all the R script files with the functions of the package.The
man/
directory which is the folder containing theRd
(R documentation) files, that is, the text files with the technical help documentation of the functions in your package.cointoss.Rproj
is an RStudio project file that is designed to make your package easy to use with RStudio..Rbuildignore
is a hidden text file used to specify files to be ignored by R when building the tar-ball or bundle of the package.
8.4 Quick Build
With the hello world content automatically created by devtools
, you can quickly build a tiny package. In this case, there’s only one function, the hello()
function, that simply prints a hi message.
If you inspect the content of the file hello.R
(inside the R/
directory), you will see some comments suggesting a list of steps and their keyboard shortcuts:
- Build and Reload Package:
Cmd + Shift + B
- Check Package:
Cmd + Shift + E
- Test Package:
Cmd + Shift + T
Alternatively, if you go to the Build tab, you will find the Install and Restart button, the Check button, and the More menu with additional building options.

Figure 8.5: Options in the Build tab
I recommend that you follow the suggested steps to see what happens: build the package and check it:
- Build and Reload Package:
Cmd + Shift + B
- Check Package:
Cmd + Shift + E
If everything went fine, you should have been able to create a toy hello world R package (with one function) that you can load with library()
:
and check that it works:
Make a donation
If you find this resource useful, please consider making a one-time donation in any amount. Your support really matters.