In a package I am developing I need to define a new unit: flight level (FL) equivalent to 100 ft.
The units
package provides the following possibility:
units::install_conversion_constant("FL", "ft", 100)
In order to make package test (devtools::test()
) and package check (devtools::test()
) both work for my unit tests using this user-defined unit, I discovered that I need to register it in the package load phase.
Here is what I did:
In zzz.R
(a new files as per "When you do need side-effects" section):
# register flight levels (FL) as a unit when loading this package
.onLoad <- function(libname, pkgname) {
# install user-define unit for flight level
units::install_conversion_constant("FL", "ft", 100)
invisible()
}
# register flight levels (FL) as a unit when loading this package
.onUnload <- function(libname, pkgname) {
# uninstall user-define unit for flight level
units::remove_symbolic_unit("FL")
invisible()
}
Failing to do that and putting the unit registration code in some R/unit-conversion.R
file makes devtools::test()
succeed but devtools::check()
fail.
Is the solution above the correct approach to register (and remove [should this be done too?]) a new unit in a package?
.onUnload
, any opinions? – Misguidance