Skip to content

Commit

Permalink
remove dependency on udunits
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Jan 11, 2018
1 parent 0f7c92f commit 2fc8867
Show file tree
Hide file tree
Showing 22 changed files with 7,672 additions and 38 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Imports:
LinkingTo:
Rcpp
Suggests:
udunits2,
NISTunits,
measurements,
xml2,
Expand Down
68 changes: 66 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,75 @@ udunits_init <- function(path, warn_on_failure = FALSE) {
.Call('_units_udunits_init', PACKAGE = 'units', path, warn_on_failure)
}

udunits_exit <- function(lo) {
.Call('_units_udunits_exit', PACKAGE = 'units', lo)
}

R_ut_parse <- function(name) {
.Call('_units_R_ut_parse', PACKAGE = 'units', name)
}

R_ut_format <- function(p, names = FALSE, definition = FALSE) {
.Call('_units_R_ut_format', PACKAGE = 'units', p, names, definition)
R_ut_get_dimensionless_unit_one <- function(name) {
.Call('_units_R_ut_get_dimensionless_unit_one', PACKAGE = 'units', name)
}

R_ut_are_convertible <- function(a, b) {
.Call('_units_R_ut_are_convertible', PACKAGE = 'units', a, b)
}

R_convert_doubles <- function(from, to, val) {
.Call('_units_R_convert_doubles', PACKAGE = 'units', from, to, val)
}

R_ut_new_dimensionless_unit <- function(name) {
.Call('_units_R_ut_new_dimensionless_unit', PACKAGE = 'units', name)
}

R_ut_scale <- function(nw, old, d) {
.Call('_units_R_ut_scale', PACKAGE = 'units', nw, old, d)
}

R_ut_offset <- function(nw, old, d) {
.Call('_units_R_ut_offset', PACKAGE = 'units', nw, old, d)
}

R_ut_divide <- function(numer, denom) {
.Call('_units_R_ut_divide', PACKAGE = 'units', numer, denom)
}

R_ut_multiply <- function(a, b) {
.Call('_units_R_ut_multiply', PACKAGE = 'units', a, b)
}

R_ut_invert <- function(a) {
.Call('_units_R_ut_invert', PACKAGE = 'units', a)
}

R_ut_raise <- function(a, i) {
.Call('_units_R_ut_raise', PACKAGE = 'units', a, i)
}

R_ut_root <- function(a, i) {
.Call('_units_R_ut_root', PACKAGE = 'units', a, i)
}

R_ut_log <- function(a, base) {
.Call('_units_R_ut_log', PACKAGE = 'units', a, base)
}

R_ut_format <- function(p, names = FALSE, definition = FALSE, ascii = FALSE) {
.Call('_units_R_ut_format', PACKAGE = 'units', p, names, definition, ascii)
}

R_ut_set_encoding <- function(enc_str) {
.Call('_units_R_ut_set_encoding', PACKAGE = 'units', enc_str)
}

R_ut_get_symbol <- function(ustr) {
.Call('_units_R_ut_get_symbol', PACKAGE = 'units', ustr)
}

R_ut_get_name <- function(ustr) {
.Call('_units_R_ut_get_name', PACKAGE = 'units', ustr)
}

4 changes: 2 additions & 2 deletions R/conversion.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Helper functions for testing if we can convert and how using either
# user-defined conversion functions or udunits.
are_convertible <- function(from, to) {
user_are_convertible(from, to) || udunits2::ud.are.convertible(from, to)
user_are_convertible(from, to) || ud.are.convertible(from, to)
}

convert <- function(value, from, to) {
value <- unclass(value)
if (user_are_convertible(from, to)) user_convert(value, from, to)
else if (udunits2::ud.are.convertible(from, to)) udunits2::ud.convert(value, from, to)
else if (ud.are.convertible(from, to)) ud.convert(value, from, to)
else NA
}

Expand Down
4 changes: 4 additions & 0 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ NULL
.onLoad = function(libname, pkgname) {
udunits_init(character(0), TRUE)
}

.onUnLoad = function(libname, pkgname) {
udunits_exit(logical(0))
}
6 changes: 3 additions & 3 deletions R/make_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
#' make_units(ohm)
#' # Note, if the printing of non-ascii characters is garbled, then you may
#' # need to specify the encoding on your system manually like this:
#' # udunits2::ud.set.encoding("latin1")
#' # ud.set.encoding("latin1")
#' # not all unit names get converted to symbols under different encodings
#'
#' ## Arithmetic operations and units
Expand Down Expand Up @@ -198,7 +198,7 @@ is_udunits_time <- function(s) {
#' case, automatic unit simplification may not work properly when performing
#' operations on unit objects, but unit conversion and other Math operations
#' should still give correct results so long as the unit string supplied
#' returns \code{TRUE} for \code{udunits2::ud.is.parsable()}.
#' returns \code{TRUE} for \code{ud.is.parsable()}.
#'
#' The second type of unit string accepted is one with implicit exponents. In
#' this format, \code{/}, \code{*}, and \code{^}, may not be present in the
Expand Down Expand Up @@ -288,7 +288,7 @@ units_eval_env$lb <- function(x) base::log(x, base = 2)
#' @rdname as_units
#'
#' @param check_is_valid throw an error if all the unit symbols are not either
#' recognized by udunits2 via \code{udunits2::ud.is.parseable()}, or a custom
#' recognized by udunits2 via \code{ud.is.parseable()}, or a custom
#' user defined via \code{install_symbolic_unit()}. If \code{FALSE}, no check
#' for validity is performed.
#'
Expand Down
25 changes: 25 additions & 0 deletions R/udunits.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ud.are.convertible = function(u1, u2) {
R_ut_are_convertible(R_ut_parse(as.character(u1)), R_ut_parse(as.character(u2)))
}

ud.get.symbol = function(u) {
sy = R_ut_get_symbol(u)
if (sy == "")
R_ut_get_name(u)
else
sy
}

ud.is.parseable = function(u) {
res <- try(R_ut_parse(u), silent = TRUE)
! inherits(res, "try-error")
}

ud.convert = function(value, from, to) {
R_convert_doubles(R_ut_parse(from), R_ut_parse(to), value)
}

ud.set.encoding = function(enc) {
R_ut_set_encoding(as.character(enc))
return()
}
1 change: 1 addition & 0 deletions R/user_conversion.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ install_symbolic_unit <- function(chr, warn = TRUE) {
"Installation not necessary and is not performed")
return(invisible(FALSE))
}
R_ut_new_dimensionless_unit(chr)
assign(chr, NULL, envir = user_defined_units)
}

Expand Down
4 changes: 2 additions & 2 deletions R/valid_udunits.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


.get_ud_xml_dir <- function() {
ud.is.parseable("m") # make sure udunits2::.onLoad() has run,
ud.is.parseable("m") # make sure udunits2 has booted
# which sets the environment varialbe UDUNITS2_XML_PATH
udunits2_dir <- dirname(Sys.getenv("UDUNITS2_XML_PATH"))
if (udunits2_dir == "")
udunits2_dir <- dirname(system.file("share/udunits2.xml", package="udunits2"))
udunits2_dir <- dirname(system.file("share/udunits2.xml", package="units"))

if (!nzchar(udunits2_dir))
warning(
Expand Down
1 change: 1 addition & 0 deletions cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -f src/Makevars src/*.o src/*.so
Loading

0 comments on commit 2fc8867

Please sign in to comment.