Teaser
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 in eval(parse(text = make_list[[index]][["code"]])): ## Spell check failed, see /tmp/Rtmpis8C5k/myFirstPackage/log/spell.Rout for details.
The package is built, tested, checked and committed into git:
list.files(path, recursive = FALSE)
## [1] "DESCRIPTION" "LICENSE"
## [3] "Makefile" "Meta"
## [5] "NAMESPACE" "NEWS.md"
## [7] "R" "README.Rmd"
## [9] "TODO.md" "devel.R"
## [11] "doc" "inst"
## [13] "log" "make.R"
## [15] "man" "man-roxygen"
## [17] "myFirstPackage.Rcheck" "myFirstPackage_0.1.0.tar.gz"
## [19] "tests" "vignettes"
r <- git2r::repository(path) summary(r)
## Local: master /tmp/Rtmpis8C5k/myFirstPackage
## Head: [ef466b5] 2020-12-02: Packager Changes
##
## Branches: 1
## Tags: 0
## Commits: 16
## Contributors: 1
## Stashes: 0
## Ignored files: 6
## Untracked files: 0
## Unstaged files: 0
## Staged files: 0
##
## Latest commits:
## [ef466b5] 2020-12-02: Packager Changes
## [09d9891] 2020-12-02: Adding `man-roxygen/return_invisibly_null.R` from template `return_invisibly_null.R` of package` packager`.
## [4a06b9b] 2020-12-02: Adding `make.R` from template `make.R` of package` packager`.
## [54b6c42] 2020-12-02: Adding `inst/runit_tests/runit-throw.R` from template `runit-throw.R` of package` packager`.
## [899b9ff] 2020-12-02: Adding `tests/runit.R` from template `runit.R` of package` packager`.
git2r::status(r)
## working directory clean
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
## RStudio README.Rmd:8,9
## gitlab README.Rmd:43
tail(readLines(file.path(path, "log", "check.Rout")), sep = "\n")
## [1] "Status: 1 WARNING"
## [2] "See"
## [3] " ‘/tmp/Rtmpis8C5k/myFirstPackage/myFirstPackage.Rcheck/00check.log’"
## [4] "for details."
## [5] ""
## [6] ""
And we see what+s 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(given = "Andreas Dominik",
## family = "Cullmann",
## role = c("aut", "cre"),
## email = "fvafrcu@mailbox.org")
## Description: What the package does (one paragraph).
## License: BSD_2_clause + file LICENSE
## URL: https://gitlab.com/fvafrcu/myFirstPackage
## Depends:
## R (>= 4.1.0)
## Suggests:
## knitr,
## rmarkdown,
## pkgload,
## testthat,
## RUnit,
## devtools,
## rprojroot
## VignetteBuilder:
## knitr
## Encoding: UTF-8
## LazyData: true
## Roxygen: list(markdown = TRUE)
## RoxygenNote: 7.1.1
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 <- "veryImportantPackage" 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")
cat(readLines(file.path(path, "DESCRIPTION")), sep = "\n")
## Package: veryImportantPackage
## Title: veryImportantPackage
## Version: 0.1.0
## Authors@R:
## person(given = "Your",
## family = "Name",
## role = c("aut", "cre"),
## email = "some@whe.re")
## Description: This is very important.
## License: BSD_2_clause + file LICENSE
## URL: https://gitlab.com/fvafrcu/veryImportantPackage
## Depends:
## R (>= 4.1.0)
## Suggests:
## knitr,
## rmarkdown,
## pkgload,
## testthat,
## RUnit,
## devtools,
## rprojroot
## VignetteBuilder:
## knitr
## Encoding: UTF-8
## LazyData: true
## Roxygen: list(markdown = TRUE)
## RoxygenNote: 7.1.1
The package’s man page is set up accordingly:
pkgload::load_all(path)
help(paste0(package_title, "-package"))
## Loading veryImportantPackage
## veryImportantPackage
##
## Description:
##
## This is very important.
##
## Details:
##
## You will find the details in
## 'vignette("An_Introduction_to_veryImportantPackage", package =
## "veryImportantPackage")'.
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 veryImportantPackage is checked into git already:
r <- git2r::repository(path) summary(r)
## Local: master /tmp/Rtmpis8C5k/veryImportantPackage
## Head: [f29b73b] 2020-12-02: Packager Changes
##
## Branches: 1
## Tags: 0
## Commits: 16
## Contributors: 1
## Stashes: 0
## Ignored files: 0
## Untracked files: 0
## Unstaged files: 0
## Staged files: 0
##
## Latest commits:
## [f29b73b] 2020-12-02: Packager Changes
## [4136c89] 2020-12-02: Adding `man-roxygen/return_invisibly_null.R` from template `return_invisibly_null.R` of package` packager`.
## [03f4ec4] 2020-12-02: Adding `make.R` from template `make.R` of package` packager`.
## [d73f2a5] 2020-12-02: Adding `inst/runit_tests/runit-throw.R` from template `runit-throw.R` of package` packager`.
## [ecc8d0e] 2020-12-02: Adding `tests/runit.R` from template `runit.R` of package` packager`.
git2r::status(r)
## working directory clean
but we have so far only built the documentation from the roxygen
comments:
list.files(file.path(path, "log"))
## [1] "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/spell.Rout" "spell"
## [3,] "log/cleanr.Rout" "cleanr"
## [4,] "log/lintr.Rout" "lint"
## [5,] "log/covr.Rout" "covr"
## [6,] "packager::get_pkg_archive_path(absolute = FALSE)" "build"
## [7,] "log/check.Rout" "check"
## [8,] ".log.Rout" ".log"
## [9,] "log/testthat.Rout" "testthat"
## [10,] "log/vignettes.Rout" "vignettes"
## [11,] "log/codetags.Rout" "codetags"
## [12,] "log/news.Rout" "news"
## [13,] "log/usage.Rout" "usage"
## [14,] "log/winbuilder.Rout" "winbuilder"
## [15,] "log/cran_comments.Rout" "cran_comments"
## [16,] "log/check_as_cran.Rout" "check_as_cran"
## [17,] "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))))
## Warning in eval(parse(text = make_list[[index]][["code"]])): ## Spell check failed, see /tmp/Rtmpis8C5k/veryImportantPackage/log/spell.Rout for details.
## [1] "log/cleanr.Rout" "log/codetags.Rout"
## [3] "log/covr.Rout" "log/lintr.Rout"
## [5] "log/news.Rout" "log/spell.Rout"
## [7] "log/testthat.Rout" "log/usage.Rout"
## [9] "log/vignettes.Rout" "veryImportantPackage_0.1.0.tar.gz"
We note the warning
cat(git2r::diff(r, as_char = TRUE, path = file.path(".Rbuildignore")))
## diff --git a/.Rbuildignore b/.Rbuildignore
## index 869d606..60acd7f 100644
## --- a/.Rbuildignore
## +++ b/.Rbuildignore
## @@ -15,3 +15,5 @@
## ^man-roxygen/return_invisibly_null\.R$
## ^\.log\.Rout$
## ^log$
## +^doc$
## +^Meta$
and see that now there are untracked files in our package’s log directory and that some files changed.
git2r::status(r)
## Untracked files:
## Untracked: log/build.Rout
## Untracked: log/cleanr.Rout
## Untracked: log/codetags.Rout
## Untracked: log/covr.Rout
## Untracked: log/lintr.Rout
## Untracked: log/news.Rout
## Untracked: log/spell.Rout
## Untracked: log/testthat.Rout
## Untracked: log/usage.Rout
## Untracked: log/vignettes.Rout
##
## Unstaged changes:
## Modified: .Rbuildignore
## Modified: .gitignore
cat(diff(r, as_char = TRUE, path = ".Rbuildignore"))
## diff --git a/.Rbuildignore b/.Rbuildignore
## index 869d606..60acd7f 100644
## --- a/.Rbuildignore
## +++ b/.Rbuildignore
## @@ -15,3 +15,5 @@
## ^man-roxygen/return_invisibly_null\.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"))
## [8a68545] 2020-12-02: make build
git2r::status(r)
## working directory clean
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] "veryImportantPackage_0.1.0.tar.gz" "log/check.Rout"
We again see new files and changes to old files.
git2r::status(r)
## Untracked files:
## Untracked: log/check.Rout
##
## Unstaged changes:
## Modified: log/build.Rout
## Modified: log/lintr.Rout
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: 1 WARNING
## See
## ‘/tmp/Rtmpis8C5k/veryImportantPackage/veryImportantPackage.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"))
## [6085b78] 2020-12-02: make check
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
## 0.568 0.004 0.573
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 provide comments to CRAN:
withr::with_dir(path, print(fakemake::make("cran_comments", ml, verbose = FALSE)))
## R CMD check results
## 0 errors | 1 warning | 0 notes
## Warning in get_gitlab_info(path = path, private_token = private_token): You need ## a private token to access gitlab.
## [1] "log/cran_comments.Rout"
cat(readLines(file.path(path, "cran-comments.md")), sep = "\n")
## Dear CRAN Team,
## this is a resubmission of package 'veryImportantPackage'. I have added the following changes:
##
## * Added a `NEWS.md` file to track changes to the package.
##
##
## Please upload to CRAN.
## Best, Your
##
## # Package veryImportantPackage 0.1.0
##
## Reporting is done by packager version 1.8.0
##
##
## ## Test environments
## - R Under development (unstable) (2020-11-20 r79451)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Devuan GNU/Linux 3 (beowulf)
## NA
## - win-builder (devel)
##
## ## Local test results
## - Testthat:
##
## - Coverage by covr:
##
##
## ## Local meta results
## - lintr:
## found 1 lints in 32 lines of code (a ratio of 0.0312).
## - cleanr:
## found 0 dreadful things about your code.
## - codetools::checkUsagePackage:
## found 0 issues.
## - devtools::spell_check:
## found 2 unkown words.
After editing the contents we feel ready to submit:
try(packager::submit(path))
## Error in packager::submit(path) : You have uncommitted changes.
Oops: we need to commit git first:
packager::git_add_commit(path = path, untracked = TRUE,
message = "prepare for CRAN")
## [3169858] 2020-12-02: prepare for CRAN
git2r::status(r)
## working directory clean
Now we try and fail again, because this vignette is built in batch mode and there’s a security query which then fails:
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")
## [316985] 0.1.0
packager::use_dev_version(path = path)
## Package version bumped from '0.1.0' to '0.1.0.9000'
## [c5b9140] 2020-12-02: Using Developement Version
desc::desc_get("Version", file = path)
## Version
## "0.1.0.9000"
cat(readLines(file.path(path, "NEWS.md")), sep = "\n")
## # veryImportantPackage 0.1.0.9000
##
## * FIXME
##
## # veryImportantPackage 0.1.0
##
## * Added a `NEWS.md` file to track changes to the package.