Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,24 @@
* Fix spacing in `str()` output for `units` objects with R >= 4.7, where
`str()` no longer starts with a blank; #450

* Rework `install_unit()` and `remove_unit()`; #449 addressing #448:
* Rework `install_unit()` and `remove_unit()`; #449, #452 addressing #448, #451:
- an installation either succeeds as a whole or maps nothing;
- symbols and names for units that already have one are installed as
aliases, e.g., `install_unit("deg2", "1 degree")`;
- symbols and names that udunits already reads as a different unit are
refused, as they would otherwise silently redefine it (e.g., a name `"m"`,
or a symbol `"km"`) or never be reached (e.g., a symbol `"gal"`, which the
database name wins over), including names that only differ in case from a
symbol (e.g., `"KG"`), and so are ids that udunits does not read back as
the new unit (e.g., `"zz1"`);
- `NA`, empty, and invalid strings in the current locale are refused;
- `remove_unit()` undoes only what `install_unit()` did, so it no longer
strips the symbol or name of the unit an alias points to, and it leaves
symbols and names from the database untouched with a warning;
- `NA`, empty, and invalid strings in the current locale are refused.
- new argument `override` to redefine symbols and names from the database
on purpose, e.g., `install_unit("gal", "1 gallon", override = TRUE)`; the
database mappings set aside are reported, and `remove_unit()` restores
them.

# version 1.0-1

Expand Down
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ R_ut_format <- function(p, names = FALSE, definition = FALSE, ascii = FALSE) {
.Call('_units_R_ut_format', PACKAGE = 'units', p, names, definition, ascii)
}

ud_install <- function(symbols, names, unit) {
invisible(.Call('_units_ud_install', PACKAGE = 'units', symbols, names, unit))
ud_install <- function(symbols, names, unit, override = FALSE) {
.Call('_units_ud_install', PACKAGE = 'units', symbols, names, unit, override)
}

ud_remove <- function(symbols, names) {
Expand Down
56 changes: 47 additions & 9 deletions R/user_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@
#' Otherwise, the first symbol and the first name become those of the unit.
#'
#' An installation either succeeds as a whole or leaves the system unchanged:
#' if any symbol or name already maps to a different unit, nothing is
#' installed. Removing a symbol or name only undoes what \code{install_unit}
#' did for it; symbols and names from the units database, or from anywhere
#' else, are left untouched with a warning.
#' if any symbol or name is already read as a different unit, nothing is
#' installed. Note that UDUNITS-2 reads an identifier by name (regardless of
#' case), then by symbol, and then by removing a prefix, so a new symbol or
#' name also collides with names or symbols of the other kind, with prefixed
#' units (e.g., \code{"km"}), and, for names, with symbols that differ only in
#' case (e.g., a name \code{"KG"} would shadow \code{"kg"}). Removing a symbol
#' or name only undoes what \code{install_unit} did for it; symbols and names
#' from the units database, or from anywhere else, are left untouched with a
#' warning.
#'
#' To redefine a symbol or name from the database on purpose, use
#' \code{override = TRUE}: the database mappings that give it another meaning
#' are set aside (and reported with a message), and \code{remove_unit} puts
#' them back. The new meaning also applies to prefixed forms (e.g., redefining
#' \code{"gal"} redefines \code{"kgal"}), and to existing \code{units} objects
#' that use the identifier, so it is best done at the start of a session.
#' Identifiers installed by \code{install_unit} cannot be overridden, but must
#' be removed first, and other identifiers that a new name would shadow are
#' still refused. As names are matched regardless of case, overriding a symbol
#' also sets aside a database name spelled the same in any letter case (e.g.,
#' overriding the symbol \code{"gal"} makes \code{"Gal"} unavailable until it
#' is removed); override the name instead to redefine every spelling.
#'
#' The \code{def} argument enables arbitrary relationships with existing
#' units using UDUNITS-2 syntax:
Expand Down Expand Up @@ -46,6 +64,8 @@
#' \item a relationship with existing units (see details for the syntax).
#' }
#' @param name a vector of names to be installed/removed.
#' @param override logical; if \code{TRUE}, symbols and names that are read as a
#' different unit in the units database are redefined (see details).
#'
#' @examples
#' # define a fortnight
Expand Down Expand Up @@ -78,9 +98,18 @@
#' # clean up
#' remove_unit(c("cell", cfu_symbols), cfu_names)
#'
#' # redefine "gal" (in the database, the unit of acceleration cm/s^2)
#' install_unit("gal", "1 gallon", override = TRUE)
#' set_units(set_units(1, gal), L)
#' # clean up: "gal" is cm/s^2 again
#' remove_unit("gal")
#' set_units(set_units(1, gal), cm/s^2)
#'
#' @export
install_unit <- function(symbol=character(0), def=character(0), name=character(0)) {
install_unit <- function(symbol=character(0), def=character(0), name=character(0),
override=FALSE) {
stopifnot(is.character(def), length(def) < 2)
stopifnot(is.logical(override), length(override) == 1, !is.na(override))
.check_ids(symbol, name)

# workaround for https://github.com/r-quantities/units/issues/289
Expand All @@ -96,7 +125,10 @@ install_unit <- function(symbol=character(0), def=character(0), name=character(0
ut_unit <- R_ut_parse(def)
}

ud_install(symbol, name, ut_unit)
was <- ud_install(symbol, name, ut_unit, override)
if (length(was))
message("redefined ", paste(was, collapse = ", "))
invisible(NULL)
}

.check_ids <- function(symbol, name) {
Expand All @@ -112,9 +144,15 @@ install_unit <- function(symbol=character(0), def=character(0), name=character(0
remove_unit <- function(symbol=character(0), name=character(0)) {
.check_ids(symbol, name)

foreign <- ud_remove(symbol, name)
if (length(foreign))
res <- ud_remove(symbol, name)
if (length(res$lost)) # nocov start
warning("could not restore the database ", paste(res$lost, collapse = ", "),
"; reload the database with load_units_xml()", call. = FALSE)
# nocov end
if (length(res$foreign))
warning("not installed by install_unit(), left unchanged: ",
paste0("'", foreign, "'", collapse = ", "), call. = FALSE)
paste0("'", res$foreign, "'", collapse = ", "),
"; use install_unit(..., override = TRUE) to redefine them",
call. = FALSE)
invisible(NULL)
}
38 changes: 33 additions & 5 deletions man/install_unit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,20 @@ BEGIN_RCPP
END_RCPP
}
// ud_install
void ud_install(CharacterVector symbols, CharacterVector names, xut_unit unit);
RcppExport SEXP _units_ud_install(SEXP symbolsSEXP, SEXP namesSEXP, SEXP unitSEXP) {
CharacterVector ud_install(CharacterVector symbols, CharacterVector names, xut_unit unit, bool override);
RcppExport SEXP _units_ud_install(SEXP symbolsSEXP, SEXP namesSEXP, SEXP unitSEXP, SEXP overrideSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::traits::input_parameter< CharacterVector >::type symbols(symbolsSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type names(namesSEXP);
Rcpp::traits::input_parameter< xut_unit >::type unit(unitSEXP);
ud_install(symbols, names, unit);
return R_NilValue;
Rcpp::traits::input_parameter< bool >::type override(overrideSEXP);
rcpp_result_gen = Rcpp::wrap(ud_install(symbols, names, unit, override));
return rcpp_result_gen;
END_RCPP
}
// ud_remove
CharacterVector ud_remove(CharacterVector symbols, CharacterVector names);
List ud_remove(CharacterVector symbols, CharacterVector names);
RcppExport SEXP _units_ud_remove(SEXP symbolsSEXP, SEXP namesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Expand Down Expand Up @@ -288,7 +290,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_units_R_ut_log", (DL_FUNC) &_units_R_ut_log, 2},
{"_units_R_ut_parse", (DL_FUNC) &_units_R_ut_parse, 1},
{"_units_R_ut_format", (DL_FUNC) &_units_R_ut_format, 4},
{"_units_ud_install", (DL_FUNC) &_units_ud_install, 3},
{"_units_ud_install", (DL_FUNC) &_units_ud_install, 4},
{"_units_ud_remove", (DL_FUNC) &_units_ud_remove, 2},
{NULL, NULL, 0}
};
Expand Down
Loading
Loading