Skip to content

Commit

Permalink
conversion to base, addresses #132
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Jul 1, 2018
1 parent 5536c0a commit e13a1ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
12 changes: 10 additions & 2 deletions R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ assign(".units.auto_convert_names_to_symbols", NA, envir = .units_options)
assign(".units.simplify", NA, envir = .units_options)
assign(".units.allow_mixed", NA, envir = .units_options)
assign(".units.unitless_symbol", NA, envir = .units_options)
assign(".units.convert_to_base", NA, envir = .units_options)


#' set one or more units global options
Expand All @@ -23,6 +24,7 @@ assign(".units.unitless_symbol", NA, envir = .units_options)
#' @param simplify logical, default \code{NA}; simplify units in expressions?
#' @param allow_mixed logical; if \code{TRUE}, combining mixed units creates a \code{mixed_units} object, if \code{FALSE} it generates an error
#' @param unitless_symbol character; set the symbol to use for unitless (1) units
#' @param convert_to_base logical; if \code{TRUE}, convert units to (SI) base units
#' @details This sets or gets units options. Set them by using named arguments, get them by passing the option name.
#'
#' The default \code{NA} value for \code{simplify} means units are not simplified in \link{set_units} or \link{as_units}, but are simplified in arithmetical expressions.
Expand All @@ -35,7 +37,7 @@ assign(".units.unitless_symbol", NA, envir = .units_options)
#' units_options("group")
#' @export
units_options = function(..., sep, group, negative_power, parse, set_units_mode, auto_convert_names_to_symbols, simplify,
allow_mixed, unitless_symbol) {
allow_mixed, unitless_symbol, convert_to_base) {
# op = as.list(units:::.units_options)
ret = list()
if (!missing(sep)) {
Expand Down Expand Up @@ -83,6 +85,11 @@ units_options = function(..., sep, group, negative_power, parse, set_units_mode,
ret$unitless_symbol = get(".units.unitless_symbol", envir = .units_options)
assign(".units.unitless_symbol", unitless_symbol, envir = .units_options)
}
if (!missing(convert_to_base)) {
stopifnot(is.logical(convert_to_base))
ret$convert_to_base = get(".units.convert_to_base", envir = .units_options)
assign(".units.convert_to_base", convert_to_base, envir = .units_options)
}

dots = list(...)
if (length(dots)) {
Expand All @@ -105,7 +112,8 @@ units_options(
auto_convert_names_to_symbols = TRUE,
simplify = NA,
allow_mixed = FALSE,
unitless_symbol = "1") # set defaults
unitless_symbol = "1",
convert_to_base = FALSE) # set defaults

.units.simplify = function() {
get(".units.simplify", envir = .units_options)
Expand Down
13 changes: 12 additions & 1 deletion R/symbolic_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ as.character.symbolic_units <- function(x, ...,
paste(num_str, denom_str, sep = sep)
}

to_base <- function(x) { # https://github.com/r-quantities/units/issues/132
u_str = as.character(units(x))
u = R_ut_parse(u_str)
ft = R_ut_format(u, ascii = TRUE)
new = as_units(strsplit(ft, " ")[[1]][2])
set_units(x, new, mode = "standard")
}

.simplify_units <- function(value, sym_units) {

simplify = .units.simplify()
Expand All @@ -131,9 +139,12 @@ as.character.symbolic_units <- function(x, ...,
return(value)
}

if (isTRUE(units_options("convert_to_base")))
return(to_base(value))

# This is just a brute force implementation that takes each element in the
# numerator and tries to find a value in the denominator that can be converted
# to the same unit. It modifies "value" to rescale the nominator to the denomiator
# to the same unit. It modifies "value" to rescale the nominator to the denominator
# before removing matching units.

drop_ones = function(u) u[ u != "1" ]
Expand Down
5 changes: 4 additions & 1 deletion man/units_options.Rd

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

0 comments on commit e13a1ab

Please sign in to comment.