Chapter 2 R package
When you need to develop a package, you need some tools for that task. Since I am going to illustrate to develop R package inside Rstudio, you ought to know some basic tools required to accomplish the task. Table 2.1 shows the package you need to install them to be able to work smoothly on package development. The version of the software or package is also shown in table 2.1 for you to check the version you have in your machine and whether you need to update them.
Software/Package | Version |
---|---|
R | 4.0.3 |
Rstudio | 1.4 |
devtools
|
2.3.2 |
usethis
|
1.6.3 |
tidyverse
|
1.3.0 |
rlang
|
0.4.8 |
broom
|
0.7.1 |
R package development has become substantially easier in recent years with the introduction of a packages like devtools (Wickham, Hester, and Chang 2020) and usethis (Wickham and Bryan 2020). These packages includes a variety of functions that facilitate software development in R. During package development stages, you will find that some functions you use once (single use) and others are used several times (multiple usage). Table 2.2 highlights some functions and whether are single used and multiple uses during package development
Function | usage | Purpose |
---|---|---|
usethis::create_package()
|
Single | Initialize package |
usethis::use_mit_license()
|
Single | Add license to package |
usethis::use_pipe()
|
Single | Add pipe function as dependency |
devtools::check()
|
Multiple | build package locally and check |
devtools::load_all()
|
Multiple | load functions into memory |
usethis::use_r()
|
Multiple | create R script for function |
usethis::use_package()
|
Multiple | add package dependency |
devtools::document()
|
Multiple | build and add documentation |
2.1 Package Development
We begin our package development using the function usethis::create_package()
, which basically initialize the package development.
This function create an R project with an environment for package development in a location you specified. For this case I have created a package named babye on my desktop. The step-by-step of the process is shown in figure 2.1
Now that we have a package. let’s check it. The function devtools::check
was developed for this task as it updates the package documentation, build the package and submit over 50 checks for metadata, structure, R code, documentation and more. This can take a while to run, depending on how big your package is. It is helpful to run frequently, especially if you are planning on submitting to CRAN. But even for internal packages, it is still good practice. A screenshot of our first check is shown in figure 2.2 and spotted a single warning that our package needs a license in the DESCRIPTION file
We can fix this error by simply add a license on the package project folder. A standard recommendation is the MIT license because of its widely usage and permissions.
Once we run the usethis::use_mit_license(name = "Masumbuko Semba")
in the command line, it updates the description file and creates two new license files in the project folder (see figure 2.3). It is recommended that the two licenses files never edited.
Though the tools have added key parts of the DESCTIPTION file, you need to open it and complete the remaining parts and updates the fields with descriptive information. Once you have done that you can re-submit with devtools::check()
We notice that our package is error-free
References
Wickham, Hadley, and Jennifer Bryan. 2020. Usethis: Automate Package and Project Setup. https://CRAN.R-project.org/package=usethis.
Wickham, Hadley, Jim Hester, and Winston Chang. 2020. Devtools: Tools to Make Developing R Packages Easier. https://CRAN.R-project.org/package=devtools.