Get startedGet started for free

Package dependencies

1. Package dependencies

Welcome back. In this video, we will learn about package dependencies and how to add them to our package.

2. R package structure

Recall that dependencies are listed in the DESCRIPTION file.

3. Three types of dependencies

Before we dive into adding dependencies to our package, let's understand three different types of dependencies in R packages. Imports are packages that are required for our functions to work properly. They are automatically loaded when our package is loaded, ensuring that all the necessary functions are available.

4. Three types of dependencies

Suggests are packages that are not required for our package to function, but they provide additional functionality or examples. Users may find them useful, but they are not necessary for the core functionality of our package. Using Suggests is a courtesy to users, avoiding downloading difficult-to-install packages.

5. Three types of dependencies

Depends attaches the dependency to the package. Depends should rarely be used, because a good package is self-contained. Imports should be used instead in almost all cases.

6. Add CRAN package as dependency

Suppose our package unitConverter allows for different formats for text arguments such as all capitals or some capitals. We then could include the CRAN package stringr for character manipulation as an imported package. Thus, when the unitConverter package is loaded via the library function, stringr and all its functionality will also be loaded. We add a dependency with usethis's use_package function. Its type argument is "Imports" by default, but we can specify it here for clarity. The output tells us that stringr has been added to Imports in the DESCRIPTION file and the tip to use double colons when referencing stringr functions in our package development. The specific lines added to the DESCRIPTION file for importing stringr are given here.

7. Set minimum package version as dependency

CRAN packages are updated all the time. Suppose we are interested in having our package users have access to functions from a specific package version. We can specify a min_version set to the desired package version number in use_package. The output of the function looks the same as on the previous slide here. Note that the entry in the DESCRIPTION file specifies that a minimum stringr package version is necessary when installing unitConverter.

8. Using use_package() for Suggests

In contrast to imported packages, we can use suggested packages for their datasets as examples in our package or additional functionality like plotting. Since our package includes data frames, we may want to suggest including the tibble package for supporting tidy data frames. Using the use_package function with first argument tibble and type Suggests accomplishes this. The first output message here is a little different since tibble is not imported but just suggested. This provides a way to see if tibble is installed.

9. Using use_package() for Depends

The glue package has few dependencies and can also help with text, so it could be added to Depends. The output message suggests this be avoided though, and to use Imports instead.

10. Revisiting DESCRIPTION - first part

Let's now take a peek at what our DESCRIPTION file looks like. The first part remains largely the same as when we generated it initially. One update is our license has been set to MIT. The DESCRIPTION file can be edited directly for the Title and Description entries, but we will learn how to do that later in the course.

11. Revisiting DESCRIPTION - second part

The next part is where the major updates to the DESCRIPTION file have happened. Roxygen is used for documentation and will be the focus of the next chapter. Our package depends on a minimum version of R. Loading in of data in a lazy way is also set here. This means the datasets will be lazily loaded, so they will not occupy any memory until use. Lastly, stringr is imported and tibble is suggested.

12. Let's practice!

Test out your dependency knowledge next!