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:
The package is built, tested, checked and committed into git:
## [1] "DESCRIPTION" "LICENSE"
## [3] "Makefile" "Meta"
## [5] "NAMESPACE" "NEWS.md"
## [7] "R" "README.Rmd"
## [9] "README.md" "TODO.md"
## [11] "devel" "devel.R"
## [13] "doc" "inst"
## [15] "log" "make.R"
## [17] "man" "man-roxygen"
## [19] "myFirstPackage.Rcheck" "myFirstPackage_0.1.0.tar.gz"
## [21] "tests" "vignettes"
## # A tibble: 0 × 3
## # ℹ 3 variables: file <chr>, status <chr>, staged <lgl>
## # A tibble: 24 × 6
## commit author time files merge message
## * <chr> <chr> <dttm> <int> <lgl> <chr>
## 1 0d2801996286ef96a161bc4e36d6a… fooba… 2025-02-15 04:56:23 31 FALSE "Packa…
## 2 8fc00cf132bd7bc86c51f1b1a846b… fooba… 2025-02-15 04:55:48 2 FALSE "Addin…
## 3 5ce4780592f22335ba75067942a81… fooba… 2025-02-15 04:55:48 2 FALSE "Addin…
## 4 dd9b368e1d93ac6c3a1d42db010d1… fooba… 2025-02-15 04:55:48 2 FALSE "Addin…
## 5 08e65a2af3191840fbeafb649b79e… fooba… 2025-02-15 04:55:48 2 FALSE "Addin…
## 6 8bf215b806f05fa0e9826578c20a6… fooba… 2025-02-15 04:55:48 2 FALSE "Addin…
## 7 6af9868689bdaa4a328beb059a2b3… fooba… 2025-02-15 04:55:48 2 FALSE "Addin…
## 8 cdaccd0e0238ecc4016a8216f2fc2… fooba… 2025-02-15 04:55:48 2 FALSE "Addin…
## 9 403032db764fc15a6eb38f4310cc6… fooba… 2025-02-15 04:55:48 1 FALSE "Addin…
## 10 b0b79a89bd2bc9379c753599e73de… fooba… 2025-02-15 04:55:48 1 FALSE "Addin…
## # ℹ 14 more rows
We can look at some of the files
(the directory myFirstPackage.Rcheck
might be of interest):
## WORD FOUND IN
## RStudio README.Rmd:8,9
## README.md:8,9
## gitlab README.Rmd:43
## README.md:43
## Spell check failed, see /tmp/RtmpLmRobU/myFirstPackage/log/spell.Rout for details.
## [1] "Status: 1 ERROR, 2 WARNINGs, 5 NOTEs"
## [2] ""
## [3] "See"
## [4] " ‘/tmp/RtmpLmRobU/myFirstPackage/myFirstPackage.Rcheck/00check.log’"
## [5] "for details."
## [6] ""
And we see what is left to do:
## - make sure https://gitlab.com/foobar/myFirstPackage exists!
Customizing
We see that the package’s DESCRIPTION is filled with default values.
## Package: myFirstPackage
## Title: What it Does (One Line, Title Case)
## Version: 0.1.0
## Authors@R:
## person("Foo", "Bar", , "[email protected]", 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/foobar/myFirstPackage
## Depends:
## R (>= 4.4.0)
## Suggests:
## knitr,
## pkgload,
## rmarkdown,
## rprojroot,
## RUnit,
## testthat,
## tinytest
## VignetteBuilder: knitr
## Encoding: UTF-8
## RoxygenNote: 7.3.2
## 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
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: myOtherPackage
## Title: myOtherPackage
## Version: 0.1.0
## Authors@R:
## person("Your", "Name", , "[email protected]", role = c("aut", "cre"))
## Author: Who wrote it
## Description: This is very important.
## License: BSD_2_clause + file LICENSE
## URL: https://gitlab.com/foobar/myOtherPackage
## Depends:
## R (>= 4.4.0)
## Suggests:
## knitr,
## pkgload,
## rmarkdown,
## rprojroot,
## RUnit,
## testthat,
## tinytest
## VignetteBuilder: knitr
## Encoding: UTF-8
## RoxygenNote: 7.3.2
## Imports:
## fritools (>= 1.3.0)
The package’s man page is set up accordingly:
## myOtherPackage
##
## Description:
##
## This is very important.
##
## Details:
##
## You will find the details in
## 'vignette("An_Introduction_to_myOtherPackage", package =
## "myOtherPackage")'.
##
## Author(s):
##
## *Maintainer*: Your Name <mailto:[email protected]>
##
## See Also:
##
## Useful links:
##
## • <https://gitlab.com/foobar/myOtherPackage>
I use
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:
## # A tibble: 0 × 3
## # ℹ 3 variables: file <chr>, status <chr>, staged <lgl>
## # A tibble: 24 × 6
## commit author time files merge message
## * <chr> <chr> <dttm> <int> <lgl> <chr>
## 1 e5b1e178f29fafacf4cc571f5e2b6… fooba… 2025-02-15 04:56:24 14 FALSE "Packa…
## 2 e5222e11dd4edf4322151618b3561… fooba… 2025-02-15 04:56:24 2 FALSE "Addin…
## 3 3605aa9b0e66c310d282ad0682a01… fooba… 2025-02-15 04:56:24 2 FALSE "Addin…
## 4 2e58305febca26fe927681c19b328… fooba… 2025-02-15 04:56:24 2 FALSE "Addin…
## 5 aefd6ca7f183b3f136a087cc818fa… fooba… 2025-02-15 04:56:24 2 FALSE "Addin…
## 6 1b1ded36aa16b9d3a6e58d57563fd… fooba… 2025-02-15 04:56:24 2 FALSE "Addin…
## 7 ae163dd9a59ed9e94b929b869dac9… fooba… 2025-02-15 04:56:24 2 FALSE "Addin…
## 8 8cf9bdb24fb589690418558f1eed3… fooba… 2025-02-15 04:56:24 2 FALSE "Addin…
## 9 cbb0eb0eda2613aec6e0962e94349… fooba… 2025-02-15 04:56:24 1 FALSE "Addin…
## 10 7b04803b154c7deeeaa3cad025b29… fooba… 2025-02-15 04:56:24 1 FALSE "Addin…
## # ℹ 14 more rows
but we have so far only built the documentation from the roxygen
comments:
## [1] "dependencies.Rout" "roxygen2.Rout"
So we get a makelist
and look at its targets and aliases:
## [,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:
## [1] "README.md" "log/check_codetags.Rout"
## [3] "log/cleanr.Rout" "log/covr.Rout"
## [5] "log/lintr.Rout" "log/news.Rout"
## [7] "log/news_rd.Rout" "log/spell.Rout"
## [9] "log/testthat.Rout" "log/tinytest.Rout"
## [11] "log/usage.Rout" "log/vignettes.Rout"
## [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.
## # A tibble: 18 × 3
## file status staged
## <chr> <chr> <lgl>
## 1 .Rbuildignore modified FALSE
## 2 .gitignore modified FALSE
## 3 README.md new FALSE
## 4 inst/NEWS.rd new FALSE
## 5 inst/vignettes_code/ new FALSE
## 6 log/build.Rout new FALSE
## 7 log/check_codetags.Rout new FALSE
## 8 log/cleanr.Rout new FALSE
## 9 log/covr.Rout new FALSE
## 10 log/lintr.Rout new FALSE
## 11 log/news.Rout new FALSE
## 12 log/news_rd.Rout new FALSE
## 13 log/readme.Rout new FALSE
## 14 log/spell.Rout new FALSE
## 15 log/testthat.Rout new FALSE
## 16 log/tinytest.Rout new FALSE
## 17 log/usage.Rout new FALSE
## 18 log/vignettes.Rout new FALSE
## diff --git a/.Rbuildignore b/.Rbuildignore
## index 8a5d5c8..000c96d 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:
## [1] "869fd39f8253684e25ed00a192ded10d4aaea8ee"
## # A tibble: 0 × 3
## # ℹ 3 variables: file <chr>, status <chr>, staged <lgl>
Checking the Package
So now we want the check the package:
## [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.
## # A tibble: 3 × 3
## file status staged
## <chr> <chr> <lgl>
## 1 log/check.Rout new FALSE
## 2 log/testthat.Rout modified FALSE
## 3 log/tinytest.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:
## * DONE
## Status: 1 ERROR, 2 WARNINGs, 5 NOTEs
##
## See
## ‘/tmp/RtmpLmRobU/myOtherPackage/myOtherPackage.Rcheck/00check.log’
## for details.
and commit again
## [1] "a6c20ee3e9fcc0db2f4eb1c75a7e777eff3ca2b4"
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:
## NULL
## user system elapsed
## 0.637 0.008 0.645
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:
## 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:
## [1] "fff8fc77b48ca825ea4499f2a44525d675cce274"
## Version
## "0.1.0.9000"
## # myOtherPackage 0.1.0.9000
##
## * FIXME
##
## # myOtherPackage 0.1.0
##
## * Added a `NEWS.md` file to track changes to the package.