Creating R Packages

Step-by-step instructions how to develop, test and create R packages using devtools, roxygen2 and testthat

Tobias Byland www.byland.info
2022-11-05

Learning how to create my own R packages was always something I wanted to learn but never really got around to doing.

Until now.

Now the really cool thing is that there already exists a lot of very good documentation about this topic, but this also begs the question: Why am I writing yet another post about this topic?

The answer is of course, as so often, a bit selfish as I learnt some time ago that I process new things faster if I write about them. Hence this blog post ;)

But maybe this post will also be helpful for somebody else who just wants a very quick intro into the topic, and then take it from there by reading up on this matter via other and more detailed sources.

This blogpost is based on the 2nd edition of the book R Packages by Hadley Wickham and Jenny Bryan.

Required packages

Initialize the package

Everything starts with calling create_package("~/my.path/packagename").

This will create a folder called packagename in ~/my.path/ which contains the required folder structure as well as the essential files for a functioning R package.

Description of the package

Edit the file ~/my.path/DESCRIPTION and change the values for title and description.

Add a license

Add a license by calling use_mit_license().

Compare ?use_mit_license for details as well as for other licensing options.

Add a function to the package

Calling use_r("functionname.R") creates and opens a new file ~/my.path/R/functionname.R in which then the function definition must be stored.

Add the function documentation

Place the cursor anywhere within the function definition and do Code > Insert roxygen skeleton.

This will add a skeletal structure in front of the function definition which can be filled out. For more details see https://roxygen2.r-lib.org/

Then run document() which will create a new file ~/my.path/man/functionname.Rd that contains the markdown file with the function documentation.

The documentation can then also be accessed by calling ?functionname.

Testcases

Initialise the unit testing machinery for the package by calling use_testthat() , which adds the folder ~/my.path/tests/testthat/ where all the unit test cases will be stored.

Then call use_test("functionname") which creates a new file tests/testthat/test-functionname.R. In this file we can add one more multiple testcases.

One way is to use the function expect_equal() , see. example below for a function squareit() which takes an integer and returns the squared value of that integer:

test_that("squareit() squares a value", {
  expect_equal(squareit(3), 9)
  expect_equal(squareit(-20), 400)
  expect_equal(squareit(1:3), c(1, 4, 9))
})
use

See https://testthat.r-lib.org/ for more information on how to write unit tests.

Now we can run all the defined unit test cases as one by calling test(). A successful result would look something like this:

Checks

Load_all

Working with the final product