Skip to content

Commit

Permalink
new arg, rename option, some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Aug 1, 2024
1 parent 670e207 commit 8d7b2d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
27 changes: 11 additions & 16 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
#'
#' @param x an \code{errors} object.
#' @param digits how many significant digits are to be used for uncertainties.
#' The default, \code{NULL}, uses \code{getOption("errors.digits", 1)}.
#' Use \code{digits="pdg"} to choose an appropriate number of digits for each
#' value according to the Particle Data Group rounding rule (see references).
#' @param scientific logical specifying whether the elements should be
#' encoded in scientific format.
#' @param notation error notation; \code{"parenthesis"} and \code{"plus-minus"}
#' are supported through the \code{"errors.notation"} option.
#' When using the "parenthesis" error notation, by default the uncertainty
#' is formatted without any decimal point, unless the option
#' \code{"errors.parenthesis.unc.dec.point"} is set to TRUE.
#' @param decimals logical specifying whether the uncertainty should be formatted
#' with a decimal point even when the \code{"parenthesis"} notation is used.
#' Otherwise (by default), the \code{"parenthesis"} notation scales the
#' uncertainty to match the least significant digit of the value.
#' @param ... ignored.
#'
#' @references
Expand All @@ -23,6 +23,7 @@
#' x <- set_errors(1:3*100, 1:3*100 * 0.05)
#' format(x)
#' format(x, digits=2)
#' format(x, digits=2, decimals=TRUE)
#' format(x, scientific=TRUE)
#' format(x, notation="plus-minus")
#'
Expand All @@ -31,15 +32,14 @@
#'
#' @export
format.errors = function(x,
digits = NULL,
digits = getOption("errors.digits", 1),
scientific = FALSE,
notation = getOption("errors.notation", "parenthesis"),
decimals = getOption("errors.decimals", FALSE),
...)
{
stopifnot(notation %in% c("parenthesis", "plus-minus"))

if (is.null(digits))
digits <- getOption("errors.digits", 1)
digits <- if (digits == "pdg") digits_pdg(.e(x)) else rep(digits, length(x))

scipen <- getOption("scipen", 0)
Expand All @@ -49,7 +49,7 @@ format.errors = function(x,
e <- signif(.e(x), digits)
nulle <- e == 0 & !is.na(e)
eexp <- get_exponent(e)
xexp <- ifelse(.v(x)== 0, eexp+1, get_exponent(x))
xexp <- ifelse(.v(x) == 0, eexp + 1, get_exponent(x))
value_digits <- ifelse(e, digits - eexp, digits)
value <- ifelse(e, signif(.v(x), xexp + value_digits), .v(x))
value <- ifelse(is.finite(value), value, .v(x))
Expand All @@ -64,14 +64,9 @@ format.errors = function(x,
if (notation == "parenthesis") {
sep <- "("
append[] <- ")"
if (!getOption("errors.parenthesis.unc.dec.point", FALSE)) {
## remove decimal point from uncertainty by scaling it appropriately
e[is.finite(e)] <- (e * 10^(pmax(0, value_digits-1)))[is.finite(e)]
} else {
## convert uncertainty for printing, keeping decimal point in line with value
e_scale_flag = (cond & eexp < xexp) | (!cond & is.finite(e) & eexp<0)
e[e_scale_flag] <- (e * 10^(pmax(0, value_digits-1)))[e_scale_flag]
}
e_scale_flag <- if (!isTRUE(decimals)) is.finite(e) else
(cond & eexp < xexp) | (!cond & is.finite(e) & eexp < 0)
e[e_scale_flag] <- (e * 10^(pmax(0, value_digits-1)))[e_scale_flag]
} else {
sep <- paste0(" ", .pm, " ")
prepend[cond] <- "("
Expand Down
17 changes: 10 additions & 7 deletions man/format.errors.Rd

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

11 changes: 3 additions & 8 deletions tests/testthat/test-print.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ test_that("error formatting works properly", {
expect_equal(format(x, notation="parenthesis", scientific=TRUE),
c("1(1000)e4", "1.1(1)e4", "1.111(1)e4", "1.1111(1)e4", "1.11112(1)e4",
"1.111122(1)e4", "1.111122222(1)e4", "1.111122222000(1)e4"))
expect_equal(format(x, notation="parenthesis", digits=3, decimals=TRUE),
c("10000(12300000)", "11110(1230)", "11111.2(12.3)", "11111.22(1.23)",
"11111.222(123)", "11111.2222(123)", "11111.2222200(123)", "11111.2222200000(123)"))

expect_equal(format(x, notation="plus-minus"), sapply(list(
c("10000", "10000000"), c("11000", "1000"), c("11110", "10"), c("11111", "1"),
Expand All @@ -41,14 +44,6 @@ test_that("error formatting works properly", {
c("(1.11112", "0.00001)e4"), c("(1.111122", "0.000001)e4"), c("(1.111122222", "0.000000001)e4"),
c("(1.111122222000", "0.000000000001)e4")),
paste, collapse=paste("", .pm, "")))
#
# test using option to keep decimal point in uncertainty in parenthesis notation
#
saved_options = options(errors.parenthesis.unc.dec.point = TRUE)
expect_equal(format(x, notation="parenthesis", digits=3),
c("10000(12300000)", "11110(1230)", "11111.2(12.3)", "11111.22(1.23)",
"11111.222(123)", "11111.2222(123)", "11111.2222200(123)", "11111.2222200000(123)"))
options(saved_options)

x <- set_errors(rep(0.827, 3), c(0.119, 0.367, 0.962))
expect_equal(format(x, notation="plus-minus", digits="pdg"), sapply(list(
Expand Down

0 comments on commit 8d7b2d4

Please sign in to comment.