Teaser

packager is a set of functions I use to create and maintain most of my R-packages using a build process such as fakemake or, GNU make. It borrows heavily from packages devtools, usethis, rcmdcheck, remotes and lintr.

withr

Due to the CRAN policy of not writing "anywhere else on the file system apart from the R session’s temporary directory", throughout this vignette I use R's temporary directory, often by using path ← file.path(tempdir(), "my_path") followed by withr::with_dir(path, …) or the like. I do this because this is a vignette and its codes are run on CRAN.

In real life, we would skip the temporary directory stuff.

Creating Packages

To create a new package I use:

path <- file.path(tempdir(), "myFirstPackage")
packager::create(path, fakemake = "check")
## Warning: `tests/testthat.R` already exists.

The package is built, tested, checked and committed into git:

list.files(path, recursive = FALSE)
##  [1] "DESCRIPTION"                 "devel"
##  [3] "devel.R"                     "doc"
##  [5] "inst"                        "LICENSE"
##  [7] "log"                         "make.R"
##  [9] "Makefile"                    "man"
## [11] "man-roxygen"                 "Meta"
## [13] "myFirstPackage_0.1.0.tar.gz" "myFirstPackage.Rcheck"
## [15] "NAMESPACE"                   "NEWS.md"
## [17] "R"                           "README.md"
## [19] "README.Rmd"                  "tests"
## [21] "TODO.md"                     "vignettes"
gert::git_status(repo = path)
## # A tibble: 0 × 3
## # … with 3 variables: file <chr>, status <chr>, staged <lgl>
## # ℹ Use `colnames()` to see all variable names
gert::git_log(repo = path)
## # A tibble: 24 × 6
##    commit                         author time                files merge message
##  * <chr>                          <chr>  <dttm>              <int> <lgl> <chr>
##  1 c8209ca1f8963c7bdd8ad2b464898… fvafr… 2022-08-07 12:12:34    31 FALSE "Packa…
##  2 1369bfca6cb8be089b812e0c3c872… fvafr… 2022-08-07 12:11:40     2 FALSE "Addin…
##  3 207c9d31547ad206bfcfb270d14a1… fvafr… 2022-08-07 12:11:40     2 FALSE "Addin…
##  4 fdcdb000bdbf0bad18b7bcb32682a… fvafr… 2022-08-07 12:11:40     2 FALSE "Addin…
##  5 3747fa39aca2717ce129807d9dc22… fvafr… 2022-08-07 12:11:40     2 FALSE "Addin…
##  6 8a0f48cdd99e9561d5166e7a43d92… fvafr… 2022-08-07 12:11:40     2 FALSE "Addin…
##  7 f829411cf4b2b4e2d9d856420cc30… fvafr… 2022-08-07 12:11:40     2 FALSE "Addin…
##  8 ffa1993170e47f7a4384092b29178… fvafr… 2022-08-07 12:11:39     2 FALSE "Addin…
##  9 888a12aeef4f7d771c6f851b6d5bc… fvafr… 2022-08-07 12:11:39     1 FALSE "Addin…
## 10 6dfe4b357b8a1ea4d880d5bf58740… fvafr… 2022-08-07 12:11:39     1 FALSE "Addin…
## # … with 14 more rows
## # ℹ Use `print(n = ...)` to see more rows

We can look at some of the files (the directory myFirstPackage.Rcheck might be of interest):

cat(readLines(file.path(path, "log", "spell.Rout")), sep = "\n")
## DESCRIPTION does not contain 'Language' field. Defaulting to 'en-US'.
##   WORD      FOUND IN
## gitlab    README.Rmd:43
## RStudio   README.Rmd:8,9
## Spell check failed, see /tmp/RtmpQnaUXs/myFirstPackage/log/spell.Rout for details.
tail(readLines(file.path(path, "log", "check.Rout")), sep = "\n")
## [1] ""
## [2] "See"
## [3] "  ‘/tmp/RtmpQnaUXs/myFirstPackage/myFirstPackage.Rcheck/00check.log’"
## [4] "for details."
## [5] ""
## [6] ""

And we see what is left to do:

cat(readLines(file.path(path, "TODO.md")), sep = "\n")
## - make sure https://gitlab.com/fvafrcu/myFirstPackage exists!

Customizing

We see that the package’s DESCRIPTION is filled with default values.

cat(readLines(file.path(path, "DESCRIPTION")), sep = "\n")
## Package: myFirstPackage
## Title: What it Does (One Line, Title Case)
## Version: 0.1.0
## Authors@R:
##     person("Andreas Dominik", "Cullmann", , "fvafrcu@mailbox.org", role = c("aut", "cre"))
## Author: Who wrote it
## Description: More about what it does (maybe more than one line)
## License: BSD_2_clause + file LICENSE
## URL: https://gitlab.com/fvafrcu/myFirstPackage
## Depends:
##     R (>= 4.3.0)
## Suggests:
##     knitr,
##     pkgload,
##     rmarkdown,
##     rprojroot,
##     RUnit,
##     testthat,
##     tinytest
## VignetteBuilder: knitr
## Encoding: UTF-8
## RoxygenNote: 7.2.1
## Imports:
##     fritools (>= 1.3.0)

We could set the package information on the existing package, but we rather create a new one now. So we get rid of our first package

unlink(path, recursive = TRUE)
if ("myFirstPackage" %in% .packages()) detach("package:myFirstPackage",
                                              unload = TRUE)

and customize the package creation (but we skip the process of testing, building and checking for the sake of CPU time, we just build the docs):

package_title <- "myOtherPackage"
path <- file.path(tempdir(), package_title)
a  <- utils::person(given = "Your", family = "Name", email = "some@whe.re",
                    role = c("aut", "cre"))
packager::create(path, author_at_r = a, title = package_title,
                 description = "This is very important.",
                 details = "At least to me.", fakemake = "roxygen2")
## Warning: `tests/testthat.R` already exists.
cat(readLines(file.path(path, "DESCRIPTION")), sep = "\n")
## Package: myOtherPackage
## Title: myOtherPackage
## Version: 0.1.0
## Authors@R:
##     person("Your", "Name", , "some@whe.re", role = c("aut", "cre"))
## Author: Who wrote it
## Description: This is very important.
## License: BSD_2_clause + file LICENSE
## URL: https://gitlab.com/fvafrcu/myOtherPackage
## Depends:
##     R (>= 4.3.0)
## Suggests:
##     knitr,
##     pkgload,
##     rmarkdown,
##     rprojroot,
##     RUnit,
##     testthat,
##     tinytest
## VignetteBuilder: knitr
## Encoding: UTF-8
## RoxygenNote: 7.2.1
## Imports:
##     fritools (>= 1.3.0)

The package’s man page is set up accordingly:

pkgload::load_all(path)
help(paste0(package_title, "-package"))
## ℹ Loading myOtherPackage
## myOtherPackage
##
## Description:
##
##      This is very important.
##
## Details:
##
##      You will find the details in
##      'vignette("An_Introduction_to_myOtherPackage", package =
##      "myOtherPackage")'.

I use

adc <- utils::person(given = "Andreas Dominik",
                      family = "Cullmann",
                      email = "fvafrcu@mailbox.org",
                      role = c("aut", "cre"))
pop <- as.list(getOption("packager"))
pop[["whoami"]] <- adc
options(packager = pop)

in one of my startup files to set the author information globally.

Maintaining Packages Using fakemake

Our brand new package myOtherPackage is checked into git already:

gert::git_status(repo = path)
## # A tibble: 0 × 3
## # … with 3 variables: file <chr>, status <chr>, staged <lgl>
## # ℹ Use `colnames()` to see all variable names
gert::git_log(repo = path)
## # A tibble: 24 × 6
##    commit                         author time                files merge message
##  * <chr>                          <chr>  <dttm>              <int> <lgl> <chr>
##  1 a96be345747e162eef6a2b67c2af2… fvafr… 2022-08-07 12:12:35    14 FALSE "Packa…
##  2 ba9c74f3581c3413d9b8dcf1b713e… fvafr… 2022-08-07 12:12:35     2 FALSE "Addin…
##  3 cbab71e77bb1c49fdbd44ed54cbfc… fvafr… 2022-08-07 12:12:35     2 FALSE "Addin…
##  4 9c85cc4b934cda473c505f9f1dd0f… fvafr… 2022-08-07 12:12:35     2 FALSE "Addin…
##  5 e753e6cc26c025859f0dfe3793415… fvafr… 2022-08-07 12:12:35     2 FALSE "Addin…
##  6 7e11b4506aac4cd5338f9c122eb6c… fvafr… 2022-08-07 12:12:35     2 FALSE "Addin…
##  7 ff4df8fed82826f0b32e163fea6be… fvafr… 2022-08-07 12:12:35     2 FALSE "Addin…
##  8 b90505922070f1f7387a40186b914… fvafr… 2022-08-07 12:12:35     2 FALSE "Addin…
##  9 c57757a261bfc3ecf1910f052a4ed… fvafr… 2022-08-07 12:12:35     1 FALSE "Addin…
## 10 6a3228ad8a977bc23b1d0892648eb… fvafr… 2022-08-07 12:12:34     1 FALSE "Addin…
## # … with 14 more rows
## # ℹ Use `print(n = ...)` to see more rows

but we have so far only built the documentation from the roxygen comments:

list.files(file.path(path, "log"))
## [1] "dependencies.Rout" "roxygen2.Rout"

So we get a makelist and look at its targets and aliases:

ml <- packager::get_package_makelist(is_cran = TRUE)
cbind(lapply(ml, function(x) x[["target"]]),
      lapply(ml, function(x) x[["alias"]]))
##       [,1]                                               [,2]
##  [1,] "log/roxygen2.Rout"                                "roxygen2"
##  [2,] "log/dependencies.Rout"                            "dependencies"
##  [3,] "log/spell.Rout"                                   "spell"
##  [4,] "log/cleanr.Rout"                                  "cleanr"
##  [5,] "log/lintr.Rout"                                   "lint"
##  [6,] "log/covr.Rout"                                    "covr"
##  [7,] "packager::get_pkg_archive_path(absolute = FALSE)" "build"
##  [8,] ".log.Rout"                                        ".log"
##  [9,] "README.md"                                        "README.md"
## [10,] "log/testthat.Rout"                                "testthat"
## [11,] "log/tinytest.Rout"                                "tinytest"
## [12,] "log/vignettes.Rout"                               "vignettes"
## [13,] "log/check_codetags.Rout"                          "check_codetags"
## [14,] "log/news_rd.Rout"                                 "news_rd"
## [15,] "log/news.Rout"                                    "news"
## [16,] "log/usage.Rout"                                   "usage"
## [17,] "log/winbuilder.Rout"                              "winbuilder"
## [18,] "log/check.Rout"                                   "check"
## [19,] "log/install.Rout"                                 "install"
## [20,] "cran-comments.md"                                 "cran_comments"
## [21,] "log/submit.Rout"                                  "submit"

Building the Package

We choose to build the package:

suppressMessages(withr::with_dir(path,
                                  print(fakemake::make("build", ml,
                                                       verbose = FALSE))))
##  [1] "log/check_codetags.Rout"     "log/cleanr.Rout"
##  [3] "log/covr.Rout"               "log/lintr.Rout"
##  [5] "log/news.Rout"               "log/news_rd.Rout"
##  [7] "log/spell.Rout"              "log/testthat.Rout"
##  [9] "log/tinytest.Rout"           "log/usage.Rout"
## [11] "log/vignettes.Rout"          "README.md"
## [13] "myOtherPackage_0.1.0.tar.gz"

We see that now there are untracked files in our package’s log directory and that some files changed.

gert::git_status(repo = path)
## # A tibble: 18 × 3
##    file                    status   staged
##    <chr>                   <chr>    <lgl>
##  1 .gitignore              modified FALSE
##  2 .Rbuildignore           modified FALSE
##  3 inst/NEWS.rd            new      FALSE
##  4 inst/vignettes_code/    new      FALSE
##  5 log/build.Rout          new      FALSE
##  6 log/check_codetags.Rout new      FALSE
##  7 log/cleanr.Rout         new      FALSE
##  8 log/covr.Rout           new      FALSE
##  9 log/lintr.Rout          new      FALSE
## 10 log/news_rd.Rout        new      FALSE
## 11 log/news.Rout           new      FALSE
## 12 log/readme.Rout         new      FALSE
## 13 log/spell.Rout          new      FALSE
## 14 log/testthat.Rout       new      FALSE
## 15 log/tinytest.Rout       new      FALSE
## 16 log/usage.Rout          new      FALSE
## 17 log/vignettes.Rout      new      FALSE
## 18 README.md               new      FALSE
packager::git_diff(x = ".Rbuildignore", path = path)
## diff --git a/.Rbuildignore b/.Rbuildignore
## index 1d22433..2363b53 100644
## --- a/.Rbuildignore
## +++ b/.Rbuildignore
## @@ -24,3 +24,5 @@
##  ^man-roxygen/stop_on_error\.R$
##  ^\.log\.Rout$
##  ^log$
## +^doc$
## +^Meta$

After inspecting the change, we commit:

withr::with_dir(path, packager::git_add_commit(path = ".", untracked = TRUE,
                                               message = "make build"))
## [1] "2177759a577d350eb1dc1bd313fe3c41024dfa8d"
gert::git_status(repo = path)
## # A tibble: 0 × 3
## # … with 3 variables: file <chr>, status <chr>, staged <lgl>
## # ℹ Use `colnames()` to see all variable names

Checking the Package

So now we want the check the package:

suppressMessages(withr::with_dir(path,
                                 print(fakemake::make("check", ml,
                                                      verbose = FALSE))))
## [1] "log/cleanr.Rout"             "log/covr.Rout"
## [3] "log/lintr.Rout"              "log/testthat.Rout"
## [5] "log/tinytest.Rout"           "myOtherPackage_0.1.0.tar.gz"
## [7] "log/check.Rout"

We again see new files and changes to old files.

gert::git_status(repo = path)
## # A tibble: 3 × 3
##   file              status   staged
##   <chr>             <chr>    <lgl>
## 1 log/check.Rout    new      FALSE
## 2 log/lintr.Rout    modified FALSE
## 3 log/testthat.Rout modified FALSE

Note that the RUnit test files are run while checking the tarball, hence we see output from RUnit in our log directory.

We assume that we passed the check:

cat(tail(readLines(file.path(path, "log", "check.Rout")), n = 7), sep = "\n")
## Status: 3 NOTEs
##
## See
##   ‘/tmp/RtmpQnaUXs/myOtherPackage/myOtherPackage.Rcheck/00check.log’
## for details.
check_log <- file.path(path, "log", "check.Rout")
status <- packager::get_check_status(check_log)
RUnit::checkEqualsNumeric(status[["status"]][["errors"]], 0)
## [1] TRUE

and commit again

withr::with_dir(path, packager::git_add_commit(path = ".", untracked = TRUE,
                                               message = "make check"))
## [1] "daa1d25f02830d2114a848e657edda2cc96e2025"

If we choose to rerun the check without touching any files "down the make chain" (i.e. no files that any of our make targets depend on), we see there’s nothing to be done:

system.time(withr::with_dir(path, print(fakemake::make("check", ml, verbose = FALSE))))
## NULL
##    user  system elapsed
##   1.341   0.004   1.346

This is the big difference between running the check via fakemake with a set of dependencies (set up with packager) and running the check (be it using R CMD check or rcmdcheck::rcmdcheck or its wrapper devtools::check) unconditionally: the latter method rebuilds and checks the whole package every time. This is why I wrote packager and fakemake.

Submitting the Package

Now we would like to submit our package to CRAN (which we will not do here, but we want to!).

We try and fail, because this vignette is built in batch mode and there’s a security query:

try(packager::submit(path))
## Warning in packager::submit(path): You have no upstream!
## Ready to submit?Error in utils::menu(qs[rand]) : menu() cannot be used non-interactively

Should you run this code interactively, you will be prompted for the security query (as you might be used from devtools::release()). Best you know how to write R extensions and the CRAN policies.

Anyway, we might want to tag the current commit and commence developing our package:

packager::git_tag(path = path, message = "A Tag")
## # A tibble: 1 × 3
##   name  ref             commit
##   <chr> <chr>           <chr>
## 1 0.1.0 refs/tags/0.1.0 a5b5491ebc1e951f6fe2ed44af5c00d38353a8ca
packager::use_dev_version(path = path)
## Package version bumped from '0.1.0' to '0.1.0.9000'
## [1] "d79a5f3b0d4f35e455f2ca7ff77e62022780f08a"
desc::desc_get("Version", file = path)
##      Version
## "0.1.0.9000"
cat(readLines(file.path(path, "NEWS.md")), sep = "\n")
## # myOtherPackage 0.1.0.9000
##
## * FIXME
##
## # myOtherPackage 0.1.0
##
## * Added a `NEWS.md` file to track changes to the package.

This is close to the workflow I have been using for most of my packages, substituting fakemake with GNU make whenever possible.