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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: units
Version: 1.0-1.6
Version: 1.0-1.7
Title: Measurement Units for R Vectors
Authors@R: c(person("Edzer", "Pebesma", role = c("aut", "cre"), email = "edzer.pebesma@uni-muenster.de", comment = c(ORCID = "0000-0001-8049-7069")),
person("Thomas", "Mailund", role = "aut", email = "mailund@birc.au.dk"),
Expand Down Expand Up @@ -42,7 +42,7 @@ SystemRequirements: udunits-2
License: GPL-2
URL: https://r-quantities.github.io/units/, https://github.com/r-quantities/units
BugReports: https://github.com/r-quantities/units/issues
RoxygenNote: 7.3.3
Roxygen: list(old_usage = TRUE)
Encoding: UTF-8
Config/testthat/edition: 3
Config/roxygen2/version: 8.1.1
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
* 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:
- 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")`;
- `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.

# version 1.0-1

* Add internal workaround for udunits2 bug with parsing of units that contain
Expand Down
24 changes: 8 additions & 16 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,6 @@ ud_convert_doubles <- function(x, from, to) {
.Call('_units_ud_convert_doubles', PACKAGE = 'units', x, from, to)
}

ud_map_names <- function(names, unit) {
invisible(.Call('_units_ud_map_names', PACKAGE = 'units', names, unit))
}

ud_unmap_names <- function(names) {
invisible(.Call('_units_ud_unmap_names', PACKAGE = 'units', names))
}

ud_map_symbols <- function(symbols, unit) {
invisible(.Call('_units_ud_map_symbols', PACKAGE = 'units', symbols, unit))
}

ud_unmap_symbols <- function(symbols) {
invisible(.Call('_units_ud_unmap_symbols', PACKAGE = 'units', symbols))
}

R_ut_get_dimensionless_unit_one <- function() {
.Call('_units_R_ut_get_dimensionless_unit_one', PACKAGE = 'units')
}
Expand Down Expand Up @@ -105,3 +89,11 @@ 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_remove <- function(symbols, names) {
.Call('_units_ud_remove', PACKAGE = 'units', symbols, names)
}

39 changes: 28 additions & 11 deletions R/user_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@
#'
#' At least one symbol or name is expected, but multiple symbols and/or names
#' can be installed (and thus mapped to the same unit) or removed at the same
#' time. The \code{def} argument enables arbitrary relationships with existing
#' time. If \code{def} resolves to a unit that already has a symbol (resp.
#' name), the new symbols (resp. names) are installed as aliases: they can be
#' used as input, but the unit keeps its own symbol (resp. name) for output.
#' 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.
#'
#' The \code{def} argument enables arbitrary relationships with existing
#' units using UDUNITS-2 syntax:
#' \tabular{llll}{
#' \strong{String Type} \tab \strong{Using Names} \tab \strong{Using Symbols}
Expand Down Expand Up @@ -70,9 +81,7 @@
#' @export
install_unit <- function(symbol=character(0), def=character(0), name=character(0)) {
stopifnot(is.character(def), length(def) < 2)
stopifnot(is.character(symbol), is.character(name))
if (!length(symbol) && !length(name))
stop("at least one symbol or name must be specified")
.check_ids(symbol, name)

# workaround for https://github.com/r-quantities/units/issues/289
if (any(grepl("%", c(symbol, name), fixed=TRUE)))
Expand All @@ -87,17 +96,25 @@ install_unit <- function(symbol=character(0), def=character(0), name=character(0
ut_unit <- R_ut_parse(def)
}

ud_map_symbols(symbol, ut_unit)
ud_map_names(name, ut_unit)
ud_install(symbol, name, ut_unit)
}

#' @rdname install_unit
#' @export
remove_unit <- function(symbol=character(0), name=character(0)) {
.check_ids <- function(symbol, name) {
stopifnot(is.character(symbol), is.character(name))
if (!length(symbol) && !length(name))
stop("at least one symbol or name must be specified")
if (!all(validEnc(c(symbol, name))))
stop("symbols and names must be valid strings in the current locale")
}

#' @rdname install_unit
#' @export
remove_unit <- function(symbol=character(0), name=character(0)) {
.check_ids(symbol, name)

ud_unmap_symbols(symbol)
ud_unmap_names(name)
foreign <- ud_remove(symbol, name)
if (length(foreign))
warning("not installed by install_unit(), left unchanged: ",
paste0("'", foreign, "'", collapse = ", "), call. = FALSE)
invisible(NULL)
}
13 changes: 12 additions & 1 deletion man/install_unit.Rd

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

5 changes: 0 additions & 5 deletions man/unitless.Rd

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

66 changes: 24 additions & 42 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,44 +85,6 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// ud_map_names
void ud_map_names(CharacterVector names, xut_unit unit);
RcppExport SEXP _units_ud_map_names(SEXP namesSEXP, SEXP unitSEXP) {
BEGIN_RCPP
Rcpp::traits::input_parameter< CharacterVector >::type names(namesSEXP);
Rcpp::traits::input_parameter< xut_unit >::type unit(unitSEXP);
ud_map_names(names, unit);
return R_NilValue;
END_RCPP
}
// ud_unmap_names
void ud_unmap_names(CharacterVector names);
RcppExport SEXP _units_ud_unmap_names(SEXP namesSEXP) {
BEGIN_RCPP
Rcpp::traits::input_parameter< CharacterVector >::type names(namesSEXP);
ud_unmap_names(names);
return R_NilValue;
END_RCPP
}
// ud_map_symbols
void ud_map_symbols(CharacterVector symbols, xut_unit unit);
RcppExport SEXP _units_ud_map_symbols(SEXP symbolsSEXP, SEXP unitSEXP) {
BEGIN_RCPP
Rcpp::traits::input_parameter< CharacterVector >::type symbols(symbolsSEXP);
Rcpp::traits::input_parameter< xut_unit >::type unit(unitSEXP);
ud_map_symbols(symbols, unit);
return R_NilValue;
END_RCPP
}
// ud_unmap_symbols
void ud_unmap_symbols(CharacterVector symbols);
RcppExport SEXP _units_ud_unmap_symbols(SEXP symbolsSEXP) {
BEGIN_RCPP
Rcpp::traits::input_parameter< CharacterVector >::type symbols(symbolsSEXP);
ud_unmap_symbols(symbols);
return R_NilValue;
END_RCPP
}
// R_ut_get_dimensionless_unit_one
xut_unit R_ut_get_dimensionless_unit_one();
RcppExport SEXP _units_R_ut_get_dimensionless_unit_one() {
Expand Down Expand Up @@ -280,6 +242,28 @@ BEGIN_RCPP
return rcpp_result_gen;
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) {
BEGIN_RCPP
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;
END_RCPP
}
// ud_remove
CharacterVector ud_remove(CharacterVector symbols, CharacterVector names);
RcppExport SEXP _units_ud_remove(SEXP symbolsSEXP, SEXP namesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::traits::input_parameter< CharacterVector >::type symbols(symbolsSEXP);
Rcpp::traits::input_parameter< CharacterVector >::type names(namesSEXP);
rcpp_result_gen = Rcpp::wrap(ud_remove(symbols, names));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_units_parse_unit", (DL_FUNC) &_units_parse_unit, 2},
Expand All @@ -289,10 +273,6 @@ static const R_CallMethodDef CallEntries[] = {
{"_units_ud_compare", (DL_FUNC) &_units_ud_compare, 4},
{"_units_ud_convertible", (DL_FUNC) &_units_ud_convertible, 2},
{"_units_ud_convert_doubles", (DL_FUNC) &_units_ud_convert_doubles, 3},
{"_units_ud_map_names", (DL_FUNC) &_units_ud_map_names, 2},
{"_units_ud_unmap_names", (DL_FUNC) &_units_ud_unmap_names, 1},
{"_units_ud_map_symbols", (DL_FUNC) &_units_ud_map_symbols, 2},
{"_units_ud_unmap_symbols", (DL_FUNC) &_units_ud_unmap_symbols, 1},
{"_units_R_ut_get_dimensionless_unit_one", (DL_FUNC) &_units_R_ut_get_dimensionless_unit_one, 0},
{"_units_R_ut_new_base_unit", (DL_FUNC) &_units_R_ut_new_base_unit, 0},
{"_units_R_ut_new_dimensionless_unit", (DL_FUNC) &_units_R_ut_new_dimensionless_unit, 0},
Expand All @@ -308,6 +288,8 @@ 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_remove", (DL_FUNC) &_units_ud_remove, 2},
{NULL, NULL, 0}
};

Expand Down
63 changes: 6 additions & 57 deletions src/udunits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Functions to support the R interface to the udunits (API version 2) library
*/

#include "units_types.h"
#include "udunits.h"

extern "C" {
int r_error_fn(const char* fmt, va_list args) {
Expand All @@ -21,13 +21,16 @@ extern "C" {

using namespace Rcpp;

static ut_system *sys = NULL;
static ut_encoding enc = UT_UTF8;
ut_system *units::sys = NULL;
ut_encoding units::enc = UT_UTF8;
using units::sys;
using units::enc;

/* High-level functions *******************************************************/

// [[Rcpp::export(rng=false)]]
void ud_exit() {
units::registry_clear();
R_gc();
ut_free_system(sys);
sys = NULL;
Expand Down Expand Up @@ -129,60 +132,6 @@ NumericVector ud_convert_doubles(NumericVector x, std::string from, std::string
return out;
}

// [[Rcpp::export(rng=false)]]
void ud_map_names(CharacterVector names, xut_unit unit) {
if (!names.size() || !unit) return;

for (int i = 0; i < names.size(); i++) {
ut_map_name_to_unit(ut_trim(names[i], UT_ASCII), UT_ASCII, unit);
ut_map_name_to_unit(ut_trim(names[i], enc), enc, unit);
}
ut_map_unit_to_name(unit, ut_trim(names[0], UT_ASCII), UT_ASCII);
ut_map_unit_to_name(unit, ut_trim(names[0], enc), enc);
}

// [[Rcpp::export(rng=false)]]
void ud_unmap_names(CharacterVector names) {
if (!names.size()) return;

xut_unit unit(ut_parse(sys, ut_trim(names[0], enc), enc));
if (!unit) return;

ut_unmap_unit_to_name(unit, enc);
ut_unmap_unit_to_name(unit, UT_ASCII);
for (int i = 0; i < names.size(); i++) {
ut_unmap_name_to_unit(sys, ut_trim(names[i], enc), enc);
ut_unmap_name_to_unit(sys, ut_trim(names[i], UT_ASCII), UT_ASCII);
}
}

// [[Rcpp::export(rng=false)]]
void ud_map_symbols(CharacterVector symbols, xut_unit unit) {
if (!symbols.size() || !unit) return;

for (int i = 0; i < symbols.size(); i++) {
ut_map_symbol_to_unit(ut_trim(symbols[i], UT_ASCII), UT_ASCII, unit);
ut_map_symbol_to_unit(ut_trim(symbols[i], enc), enc, unit);
}
ut_map_unit_to_symbol(unit, ut_trim(symbols[0], UT_ASCII), UT_ASCII);
ut_map_unit_to_symbol(unit, ut_trim(symbols[0], enc), enc);
}

// [[Rcpp::export(rng=false)]]
void ud_unmap_symbols(CharacterVector symbols) {
if (!symbols.size()) return;

xut_unit unit(ut_parse(sys, ut_trim(symbols[0], enc), enc));
if (!unit) return;

ut_unmap_unit_to_symbol(unit, enc);
ut_unmap_unit_to_symbol(unit, UT_ASCII);
for (int i = 0; i < symbols.size(); i++) {
ut_unmap_symbol_to_unit(sys, ut_trim(symbols[i], enc), enc);
ut_unmap_symbol_to_unit(sys, ut_trim(symbols[i], UT_ASCII), UT_ASCII);
}
}

/* Thin wrappers **************************************************************/

// # nocov start
Expand Down
14 changes: 14 additions & 0 deletions src/udunits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef units_udunits__h
#define units_udunits__h

#include "units_types.h"

namespace units {
// defined in udunits.cpp
extern ut_system *sys;
extern ut_encoding enc;
// defined in user_units.cpp; called before the unit system is freed
void registry_clear();
}

#endif
Loading
Loading